package com.lancoo.cvas.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.cvas.entity.*;
import com.lancoo.cvas.exception.BizException;
import com.lancoo.cvas.mapper.*;
import com.lancoo.cvas.pojo.common.SubType;
import com.lancoo.cvas.pojo.common.*;
import com.lancoo.cvas.pojo.constant.GlobalConstant;
import com.lancoo.cvas.pojo.dto.ExcelStudent;
import com.lancoo.cvas.pojo.excel.*;
import com.lancoo.cvas.pojo.vo.*;
import com.lancoo.cvas.service.*;
import com.lancoo.cvas.util.*;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Struct;
import java.text.Collator;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service("subClassService")
public class SubClassServiceImpl implements SubClassService {
    @Resource
    private TeachingClassMapper teachingClassMapper;

    @Resource
    private TeachingClassWeekMapper teachingClassWeekMapper;

    @Resource
    private TeachingClassTeacherMapper teachingClassTeacherMapper;


    @Resource
    private ClassInfoMapper classInfoMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private TaskToGradeMapper taskToGradeMapper;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private TeachingClassStudentMapper teachingClassStudentMapper;
    @Resource
    private TeachingClassService teachingClassService;
    @Resource
    private TeachingClassWeekService teachingClassWeekService;

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private TeachingClassStudentService teachingClassStudentService;
    @Resource
    private TeachingClassTeacherService teachingClassTeacherService;
    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private AssessmentMethodService assessmentMethodService;
    @Resource
    private TaskInfoService taskInfoService;
    @Resource
    private CourseService courseService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private RoomService roomService;
    @Resource
    private MajorService majorService;
    @Resource
    private PlanTeachingMapper planTeachingMapper;
    @Resource
    private SegmentService segmentService;

    @Resource
    private PlanTeachingCourseSegmentMapper planTeachingCourseSegmentMapper;
    @Resource
    private PlanTeachingStudentMapper planTeachingStudentMapper;


    /**
     * 获取拆班班级
     *
     * @param taskId
     * @param flag
     * @param gradeId
     * @param keyword
     * @return
     */
    @Override
    public List<SubClassDto> getSubClassList(Integer taskId, Integer flag, String gradeId, String keyword) {


        List<SubClassDto> subClassDto = new ArrayList<>();

        /**
         * 筛选对应的教学班
         */
        List<TeachingClass> classInfoList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(StrUtil.isNotBlank(gradeId), TeachingClass::getGradeId, gradeId)
                .eq(ObjUtil.isNotEmpty(flag), TeachingClass::getSubId, flag)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .eq(TeachingClass::getIsEnable, IsEnable.ENABLE.getCode())
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                .isNotNull(TeachingClass::getUniqueShiftSign)
                .isNotNull(TeachingClass::getSubId)
                .and(StrUtil.isNotBlank(keyword), wrapper -> wrapper.like(TeachingClass::getClassName, keyword)));

        //通过uuid进行分组
        Map<String, List<TeachingClass>> teachingClassMap = classInfoList.stream().collect(Collectors.groupingBy(TeachingClass::getUniqueShiftSign));

        teachingClassMap.forEach((key, value) -> {
            SubClassDto classDto = new SubClassDto();
            TeachingClass teachingClass = value.get(0);

            classDto.setSubId(teachingClass.getUniqueShiftSign());
            classDto.setSubName(teachingClass.getClassName());
            classDto.setSubTypeId(teachingClass.getSubId());
            classDto.setSubTypeName(teachingClass.getSubName());
            classDto.setGradeId(teachingClass.getGradeId());
            classDto.setGradeName(teachingClass.getGradeName());

            if (teachingClass.getSubId().equals(SubType.GENERAL.getCode())) {
                classDto.setCourseId(teachingClass.getCourseId());
                classDto.setCourseName(teachingClass.getCourseName());
            }
            classDto.setSubTeachingClassList(value);
            subClassDto.add(classDto);
        });
        return subClassDto;
    }

    /**
     * 删除拆班班级
     *
     * @param taskId
     * @param subIds
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String deletedSubClass(Integer taskId, List<String> subIds) {

        //获取选中的拆班班级
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(
                new LambdaQueryWrapper<TeachingClass>()
                        .eq(TeachingClass::getTaskId, taskId)
                        .in(TeachingClass::getUniqueShiftSign, subIds)
                        .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> deletedTeachingIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId)
                .distinct().collect(Collectors.toList());

        List<TeachingClass> list = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).isNotNull(TeachingClass::getClassId).isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode()));


        /**
         * 恢复之前的行政班和教学班的对应关系
         */
        ArrayList<Long> teachingClassIds = new ArrayList<>();
        for (String subId : subIds) {

            List<TeachingClass> teachingClasses = teachingClassList.stream().filter(p -> p.getUniqueShiftSign().equals(subId))
                    .collect(Collectors.toList());
            TeachingClass teachingClass = teachingClasses.get(0);
            List<Long> courseIds = teachingClassList.stream().map(TeachingClass::getCourseId).distinct()
                    .collect(Collectors.toList());

            //筛选满足条件的课程相等,并且班级相等，并且UniqueShiftSign为空
            //单双号拆班
            if (teachingClass.getSubId().equals(0)) {
                List<TeachingClass> classList = list.stream().filter(p -> courseIds.contains(p.getCourseId()) && p.getClassId().equals(teachingClass.getClassId())
                        && StringUtils.isBlank(p.getUniqueShiftSign())).collect(Collectors.toList());

                List<Long> classIds = classList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

                teachingClassIds.addAll(classIds);
                //混合常规拆班
            } else if (teachingClass.getSubId().equals(1)) {

                String classId = teachingClass.getClassId();

                List<String> classIds = Arrays.asList(classId.split(","));

                List<TeachingClass> classList = list.stream().filter(p ->courseIds.contains(p.getCourseId()) && classIds.contains(p.getClassId()))
                        .filter(p -> StringUtils.isBlank(p.getUniqueShiftSign())).collect(Collectors.toList());
                List<Long> classIds2 = classList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

                teachingClassIds.addAll(classIds2);
            }
        }

        teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                .in(TeachingClass::getTeachingClassId, teachingClassIds).set(TeachingClass::getDeleted, Deleted.NOT.getCode()));
        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds).set(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

        teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).set(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


        teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>().in(TeachingClassStudent::getTeachingClassId, teachingClassIds)
                .set(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));

        //删除需要删除的数据

        teachingClassService.lambdaUpdate().eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, deletedTeachingIds).remove();

        teachingClassWeekService.lambdaUpdate().in(TeachingClassWeek::getTeachingClassId, deletedTeachingIds).remove();

        teachingClassTeacherService.lambdaUpdate()
                .in(TeachingClassTeacher::getTeachingClassId, deletedTeachingIds).remove();

        teachingClassStudentService.lambdaUpdate()
                .in(TeachingClassStudent::getTeachingClassId, deletedTeachingIds).remove();
        return "拆班班级已删除";
    }

    /**
     * 获取拆班班级列表
     *
     * @param taskId
     * @return
     */
    @Override
    public List<ClassInfoDto> getClassList(Integer taskId) {

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .eq(TeachingClass::getIsEnable, IsEnable.ENABLE.getCode())
                .isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));


        List<Long> classIds = teachingClassList.stream().map(TeachingClass::getClassId).distinct().collect(Collectors.toList())
                .stream().map(Long::valueOf).collect(Collectors.toList());
        List<ClassInfo> classInfoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(classIds)) {
            classInfoList = classInfoMapper.selectList(new LambdaQueryWrapper<ClassInfo>().eq(ClassInfo::getTaskId, taskId)
                    .in(ClassInfo::getId, classIds));

        }

        List<ClassInfoDto> classInfoDtoList = new ArrayList<>();

        classInfoList.forEach(o -> {
            ClassInfoDto classInfoDto = new ClassInfoDto();
            classInfoDto.setClassId(o.getId());
            classInfoDto.setClassName(o.getClassName());
            classInfoDtoList.add(classInfoDto);
        });

        return classInfoDtoList.stream().sorted(Comparator.comparing(ClassInfoDto::getClassName)).collect(Collectors.toList());
    }

    /**
     * 获取拆班课程列表
     *
     * @param taskId
     * @param classId
     * @return
     */
    @Override
    public List<SubCourseDto> getSubCourse(Integer taskId, Long classId) {

        List<SubCourseDto> subCourseDtoList = new ArrayList<>();
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getClassId, String.valueOf(classId))
                .eq(TeachingClass::getFlag, 1)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getIsEnable, IsEnable.ENABLE.getCode())
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teacherClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct()
                .collect(Collectors.toList());

        if (CollUtil.isNotEmpty(teacherClassIds)) {
            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                    .in(TeachingClassWeek::getTeachingClassId, teacherClassIds));


            for (TeachingClass teachingClass : teachingClassList) {
                SubCourseDto subCourseDto = new SubCourseDto();
                subCourseDto.setCourseId(teachingClass.getCourseId());
                subCourseDto.setCourseName(teachingClass.getCourseName());
                int num = 0;

                for (TeachingClassWeek teachingClassWeek : teachingClassWeekList) {

                    if (teachingClass.getTeachingClassId().equals(teachingClassWeek.getTeachingClassId())) {
                        num = num + teachingClassWeek.getWeekNum();
                    }
                }
                if (num == 0) {
                    continue;
                }
                subCourseDto.setWeekNum(num);
                subCourseDtoList.add(subCourseDto);
            }

        }
        return subCourseDtoList;
    }

    /**
     * 第一次生成单双教学班
     *
     * @param taskId
     * @param subInputs
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addOddEvenSubClass(Integer taskId, String subId, List<SubInput> subInputs) {

        if (!checkWeekNum(subInputs)) {
            return Result.ofFail("拆班失败，需满足：周学时最大课程的周学时，小于等于其他课程周学时的总和");
        }
        SubInput subClass = new SubInput();
        Integer sum = subInputs.size();

        if (CollUtil.isNotEmpty(subInputs)) {
            subClass = subInputs.get(0);

            if (sum == 1 && (subClass.getWeekNum() % 2 != 0)) {
                return Result.ofFail("拆班失败,需满足,所选课程的周学时为2的整倍数");
            }
            if (sum.equals(1)) {
                SubInput subInput = new SubInput();
                subInput.setClassId(subClass.getClassId());
                subInput.setClassName(subClass.getClassName());
                subInput.setCourseId(subClass.getCourseId());
                subInput.setCourseName(subClass.getCourseName());
                subInput.setWeekNum(subClass.getWeekNum());
                subInputs.add(subInput);
                subInputs.forEach(o -> {
                    o.setWeekNum(o.getWeekNum() / 2);
                });
            }
        }

        Long classId = subClass.getClassId();
        List<Long> courseIds = subInputs.stream().map(SubInput::getCourseId).distinct().collect(Collectors.toList());


        //筛选出最初课程对应的教学班
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getClassId, String.valueOf(classId))
                .eq(TeachingClass::getIsEnable, IsEnable.ENABLE.getCode())
                .isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .in(TeachingClass::getCourseId, courseIds));

        //筛选出 之前拆过班后对应的教学班
        List<TeachingClass> subclassList = new ArrayList<>();
        if (StringUtils.isNotBlank(subId)) {
            subclassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                    .eq(TeachingClass::getTaskId, taskId)
                    .eq(TeachingClass::getUniqueShiftSign, subId));
        }


        List<Long> subClassIds = subclassList.stream().map(TeachingClass::getTeachingClassId).distinct()
                .collect(Collectors.toList());

        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct()
                .collect(Collectors.toList());


        List<TeachingClassStudent> teachingClassStudentList = new ArrayList<>();
        //处理学生
        List<TeachingClassStudent> teachingClassStudents = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>().in(TeachingClassStudent::getTeachingClassId, teachingClassIds));

        List<Student> studentList = studentMapper.selectList(new LambdaQueryWrapper<Student>().eq(Student::getTaskId, taskId).eq(Student::getDeleted, Deleted.NOT.getCode()));

        //添加单双号教学班
        UUID uuid = UUID.randomUUID();
        List<SubTeachingClassDto> subTeachingClassDtos = new ArrayList<>();
        List<TeachingClass> subTeachingClassList = new ArrayList<>();
        for (TeachingClass teachingClass : teachingClassList) {
            for (SubInput subInput : subInputs) {
                if (teachingClass.getCourseId().equals(subInput.getCourseId())) {

                    List<TeachingClassStudent> subStudentList = teachingClassStudents.stream().filter(p -> p.getTeachingClassId().equals(teachingClass.getTeachingClassId())).collect(Collectors.toList());
                    List<Student> students = studentList.stream().filter(p -> subStudentList.stream().anyMatch(o -> o.getStudentId().equals(p.getStudentId()))).collect(Collectors.toList());


                    SubTeachingClassDto subTeachingClassDto = new SubTeachingClassDto();
                    subTeachingClassDto.setCourseId(subInput.getCourseId());
                    subTeachingClassDto.setCourseName(subInput.getCourseName());
                    List<SubTeachingClassAdd> subTeachingClassAdds = new ArrayList<>();


                    SubTeachingClassAdd subSingTeachingClass = new SubTeachingClassAdd();
                    SubTeachingClassAdd subDoubleTeachingClass = new SubTeachingClassAdd();


                    List<Student> singClassStudents = students.stream()
                            .filter(p -> (Integer.parseInt(String.valueOf(p.getBaseStudentId()
                                    .charAt(p.getBaseStudentId().length() - 1)))) % 2 == 1).collect(Collectors.toList());
                    subSingTeachingClass.setFlag(1);
                    subSingTeachingClass.setClassId(String.valueOf(teachingClass.getClassId()));
                    subSingTeachingClass.setTeachingClassStudents(singClassStudents);

                    TeachingClass subSignClass = new TeachingClass();
                    subSignClass.setTaskId(taskId);
                    subSignClass.setClassId(String.valueOf(subInput.getClassId()));
                    subSignClass.setClassName(subClass.getClassName());
                    subSignClass.setTeachingClassName(subInput.getClassName() + subInput.getCourseName() + "(单)班");
                    subSingTeachingClass.setClassName(subSignClass.getTeachingClassName());
                    subSignClass.setTeachingPlanId(teachingClass.getTeachingPlanId());
                    subSignClass.setCourseId(subInput.getCourseId());
                    subSignClass.setCampusId(teachingClass.getCampusId());
                    subSignClass.setCampusName(teachingClass.getCampusName());
                    subSignClass.setCourseName(subInput.getCourseName());
                    subSignClass.setClassNum(teachingClass.getClassNum() % 2 == 0 ? teachingClass.getClassNum() / 2
                            : (teachingClass.getClassNum() / 2) + 1);
                    subSignClass.setGradeId(teachingClass.getGradeId());
                    subSignClass.setGradeName(teachingClass.getGradeName());
                    subSignClass.setTeachingClassType(TeachingClassType.NOSELECT.getCode());
                    subSignClass.setFlag(teachingClass.getFlag());
                    subSignClass.setCampusId(teachingClass.getCampusId());
                    subSignClass.setCampusName(teachingClass.getCampusName());
                    subSignClass.setSubId(SubType.ODDEVEN.getCode());
                    subSignClass.setSubName(SubType.ODDEVEN.getMsg());
                    subSignClass.setUniqueShiftSign(uuid.toString());
                    subSignClass.setIsSingle(IsSign.SINGLE.getCode());
                    subSignClass.setIsEnable(IsEnable.ENABLE.getCode());

                    subTeachingClassAdds.add(subSingTeachingClass);


                    List<Student> doubleClassStudents = students.stream()
                            .filter(p -> (Integer.parseInt(String.valueOf(p.getBaseStudentId()
                                    .charAt(p.getBaseStudentId().length() - 1)))) % 2 == 0).collect(Collectors.toList());
                    subDoubleTeachingClass.setFlag(0);
                    subDoubleTeachingClass.setClassId(String.valueOf(teachingClass.getClassId()));
                    subDoubleTeachingClass.setTeachingClassStudents(doubleClassStudents);
                    TeachingClass subDoubleClass = new TeachingClass();
                    subDoubleClass.setTaskId(taskId);
                    subDoubleClass.setClassId(String.valueOf(subInput.getClassId()));
                    subDoubleClass.setClassName(subClass.getClassName());
                    subDoubleClass.setTeachingClassName(subInput.getClassName() + subInput.getCourseName() + "(双)班");
                    subDoubleTeachingClass.setClassName(subDoubleClass.getTeachingClassName());
                    subDoubleClass.setTeachingPlanId(teachingClass.getTeachingPlanId());
                    subDoubleClass.setCourseId(subInput.getCourseId());
                    subDoubleClass.setCampusId(teachingClass.getCampusId());
                    subDoubleClass.setCampusName(teachingClass.getCampusName());
                    subDoubleClass.setCourseName(subInput.getCourseName());
                    subDoubleClass.setClassNum(teachingClass.getClassNum() / 2);
                    subDoubleClass.setGradeId(teachingClass.getGradeId());
                    subDoubleClass.setGradeName(teachingClass.getGradeName());
                    subDoubleClass.setTeachingClassType(TeachingClassType.NOSELECT.getCode());
                    subDoubleClass.setFlag(teachingClass.getFlag());
                    subDoubleClass.setCampusId(teachingClass.getCampusId());
                    subDoubleClass.setCampusName(teachingClass.getCampusName());
                    subDoubleClass.setCampusName(teachingClass.getCampusName());
                    subDoubleClass.setSubId(SubType.ODDEVEN.getCode());
                    subDoubleClass.setSubName(SubType.ODDEVEN.getMsg());
                    subDoubleClass.setUniqueShiftSign(uuid.toString());
                    subDoubleClass.setIsSingle(IsSign.DOUBLE.getCode());
                    subDoubleClass.setIsEnable(IsEnable.ENABLE.getCode());
                    subTeachingClassAdds.add(subDoubleTeachingClass);
                    subTeachingClassDto.setTeachingClassList(subTeachingClassAdds);
                    subTeachingClassDtos.add(subTeachingClassDto);

                    subTeachingClassList.add(subSignClass);
                    subTeachingClassList.add(subDoubleClass);
                }
            }
        }


        teachingClassService.saveBatch(subTeachingClassList);


        for (SubTeachingClassDto subTeachingClassDto : subTeachingClassDtos) {

            List<TeachingClass> teachingClasses = subTeachingClassList.stream()
                    .filter(p -> p.getCourseId().equals(subTeachingClassDto.getCourseId()))
                    .collect(Collectors.toList());

            subTeachingClassDto.getTeachingClassList().forEach(o -> {

                if (o.getFlag() == 1) {
                    List<TeachingClass> teachingClass = teachingClasses.stream().filter(p -> p.getIsSingle().equals(IsSign.SINGLE.getCode())).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(teachingClass)) {
                        teachingClass.forEach(t -> {
                            o.setTeachingClassId(t.getTeachingClassId());
                            o.getTeachingClassStudents().forEach(s -> {
                                TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
                                teachingClassStudent.setStudentId(s.getStudentId());
                                teachingClassStudent.setTeachingClassId(t.getTeachingClassId());
                                teachingClassStudentList.add(teachingClassStudent);
                            });
                        });
                    }
                } else {
                    List<TeachingClass> teachingClass = teachingClasses.stream().filter(p -> p.getIsSingle().equals(IsSign.DOUBLE.getCode())).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(teachingClass)) {

                        teachingClass.forEach(t -> {
                            o.setTeachingClassId(t.getTeachingClassId());
                            o.getTeachingClassStudents().forEach(s -> {
                                TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
                                teachingClassStudent.setStudentId(s.getStudentId());
                                teachingClassStudent.setTeachingClassId(t.getTeachingClassId());
                                teachingClassStudentList.add(teachingClassStudent);
                            });
                        });
                    }
                }
            });
        }


        List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds));


        List<TeachingClassWeek> teachingClassWeeks = new ArrayList<>();


        //teacingClassWeek
        for (TeachingClass teachingClass : subTeachingClassList) {
            for (TeachingClass aClass : teachingClassList) {
                if (aClass.getCourseId().equals(teachingClass.getCourseId())) {

                    List<TeachingClassWeek> teachingClassWeek = teachingClassWeekList.stream()
                            .filter(p -> p.getTeachingClassId().equals(aClass.getTeachingClassId())).collect(Collectors.toList());

                    if (CollUtil.isNotEmpty(teachingClassWeek)) {

                        if (sum == 1) {
                            for (TeachingClassWeek classWeek : teachingClassWeek) {
                                TeachingClassWeek tWeekList = new TeachingClassWeek();
                                tWeekList.setTeachingClassId(teachingClass.getTeachingClassId());
                                tWeekList.setWeek(classWeek.getWeek());
                                tWeekList.setWeekNum(classWeek.getWeekNum() / 2);
                                tWeekList.setConnectSection(classWeek.getConnectSection());
                                tWeekList.setConnectNumber(classWeek.getConnectNumber());
                                tWeekList.setAssessmentId(classWeek.getAssessmentId());
                                tWeekList.setAssessmentName(classWeek.getAssessmentName());
                                tWeekList.setSign(classWeek.getSign());
                                tWeekList.setStartWeek(classWeek.getStartWeek());
                                tWeekList.setEndWeek(classWeek.getEndWeek());
                                tWeekList.setHourType(classWeek.getHourType());
                                tWeekList.setSubTeachingClassId(aClass.getTeachingClassId());
                                teachingClassWeeks.add(tWeekList);
                            }
                        } else {
                            for (TeachingClassWeek classWeek : teachingClassWeek) {
                                TeachingClassWeek tWeekList = new TeachingClassWeek();
                                tWeekList.setTeachingClassId(teachingClass.getTeachingClassId());
                                tWeekList.setWeek(classWeek.getWeek());
                                tWeekList.setWeekNum(classWeek.getWeekNum());
                                tWeekList.setConnectSection(classWeek.getConnectSection());
                                tWeekList.setConnectNumber(classWeek.getConnectNumber());
                                tWeekList.setAssessmentId(classWeek.getAssessmentId());
                                tWeekList.setAssessmentName(classWeek.getAssessmentName());
                                tWeekList.setSign(classWeek.getSign());
                                tWeekList.setStartWeek(classWeek.getStartWeek());
                                tWeekList.setEndWeek(classWeek.getEndWeek());
                                tWeekList.setHourType(classWeek.getHourType());
                                tWeekList.setSubTeachingClassId(aClass.getTeachingClassId());
                                teachingClassWeeks.add(tWeekList);
                            }
                        }

                    }
                }
            }
        }


        teachingClassWeekService.saveBatch(teachingClassWeeks);

        List<TeachingClassStudent> collect = teachingClassStudentList.stream().distinct().collect(Collectors.toList());
        teachingClassStudentService.saveBatch(collect);


        if (CollUtil.isNotEmpty(subClassIds)) {
            teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                    .eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                    .in(TeachingClass::getTeachingClassId, subClassIds).set(TeachingClass::getDeleted, Deleted.YES.getCode()));

            teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                    .in(TeachingClassWeek::getTeachingClassId, subClassIds).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));


            teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                    .in(TeachingClassTeacher::getTeachingClassId, subClassIds).set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));

            teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                    .in(TeachingClassStudent::getTeachingClassId, subClassIds).set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));
        }

        teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                .in(TeachingClass::getTeachingClassId, teachingClassIds).set(TeachingClass::getDeleted, Deleted.YES.getCode()));

        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));

        teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));


        teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                .in(TeachingClassStudent::getTeachingClassId, teachingClassIds).set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));


        return Result.ofSuccess(uuid.toString());

    }

    /**
     * 获取教学班对应树
     *
     * @param taskId
     * @return
     */
    @Override
    public List<TeachingClassTree> getTeachingClassTree(Integer taskId, Integer module) {
        List<TeachingClassTree> teachingClassTreeList = new ArrayList<>();

        //普通教学班
        TeachingClassTree teachingClassTreeClassInfo = new TeachingClassTree();
        //选课教学班
        TeachingClassTree teachingClassTreeSelectInformation = new TeachingClassTree();
        //拆班教学班
        TeachingClassTree teachingClassTreeSubInformation = new TeachingClassTree();
        //合班教学班
        TeachingClassTree teachingClassTreeMixedClassInfo = new TeachingClassTree();

        List<GradeClassInformation> gradeClassInformations = new ArrayList<>();
        List<GradeClassInformation> selectGradeInfos = new ArrayList<>();
        List<GradeClassInformation> subGradeInfos = new ArrayList<>();
        List<GradeClassInformation> mixedGradeInfos = new ArrayList<>();

        List<ClassInfo> classInfoList = classInfoMapper.selectList(new LambdaQueryWrapper<ClassInfo>().eq(ClassInfo::getTaskId, taskId));


        List<TaskToGrade> taskToGradeList = taskToGradeMapper.selectList(new LambdaQueryWrapper<TaskToGrade>()
                .eq(TaskToGrade::getTaskId, taskId).eq(TaskToGrade::getDeleted, Deleted.NOT.getCode())
                .orderByAsc(TaskToGrade::getGradeName));


        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(module != 0, TeachingClass::getFlag, 1)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        if (CollUtil.isNotEmpty(teachingClassList)) {

            Map<String, String> gradeMap = taskToGradeList.stream().collect(Collectors.toMap(TaskToGrade::getGradeId, TaskToGrade::getGradeName));

            if (module == 2) {
                List<Long> tcIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());
                //排课状态
                List<Long> scheduleTeachingClassIds = teachingClassWeekService.getUnScheduleTeachingClassIds(
                        tcIds,
                        ScheduleState.UNARRANGE.getCode(), ScheduleState.CONFLICT.getCode(), Deleted.NOT.getCode());

                //去除没课教学班
                Map<Long, List<TeachingClassWeek>> weekMap = teachingClassWeekService.lambdaQuery()
                        .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                        .in(TeachingClassWeek::getTeachingClassId, tcIds).list()
                        .stream().collect(Collectors.groupingBy(TeachingClassWeek::getTeachingClassId));

                teachingClassList = teachingClassList.stream().filter(o ->
                        weekMap.containsKey(o.getTeachingClassId())
                                && weekMap.get(o.getTeachingClassId()).stream()
                                .anyMatch(w -> w.getWeekNum() != null && w.getWeekNum() != 0 && w.getRoomType() != null)
                ).collect(Collectors.toList());

                teachingClassList.forEach(o -> {
                    if (scheduleTeachingClassIds.contains(o.getTeachingClassId())) {
                        o.setScheduleState(-1);
                    }
                });
            }

            if (module != 0) {
                List<Long> tcIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());
                //去除没课教学班
                Map<Long, List<TeachingClassWeek>> weekMap = teachingClassWeekService.lambdaQuery()
                        .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                        .in(TeachingClassWeek::getTeachingClassId, tcIds).list()
                        .stream().collect(Collectors.groupingBy(TeachingClassWeek::getTeachingClassId));

                teachingClassList = teachingClassList.stream().filter(o ->
                        weekMap.containsKey(o.getTeachingClassId())
                                && (o.getIsEnable() == 0 || o.getIsEnable() == 6)
                                && weekMap.get(o.getTeachingClassId()).stream()
                                .anyMatch(w -> w.getWeekNum() != null && w.getWeekNum() != 0 && w.getRoomType() != null)
                ).collect(Collectors.toList());

//                teachingClassList = teachingClassList.stream().filter(o ->
//                       o.getIsEnable() == 0 || o.getIsEnable() == 6
//                ).collect(Collectors.toList());

            }


            //行政班对应班级
            List<TeachingClass> finalTeachingClassList = teachingClassList;
            gradeMap.forEach((key, value) -> {
                GradeClassInformation gradeClassInformation = new GradeClassInformation();
                gradeClassInformation.setGradeId(key);
                gradeClassInformation.setGradeName(value);
                gradeClassInformation.setTypeName("行政班");

                List<ClassInform> classInformList = new ArrayList<>();
                List<TeachingClass> teachingClasses = finalTeachingClassList.stream().filter(p -> p.getGradeId().equals(key)
                        && StringUtils.isBlank(p.getUniqueShiftSign()) && p.getTeachingClassType().equals(TeachingClassType.NOSELECT.getCode()))
                        .collect(Collectors.toList());

                List<Long> collect = teachingClasses.stream().map(TeachingClass::getClassId).map(Long::valueOf).distinct().collect(Collectors.toList());

                List<ClassInfo> collect1 = classInfoList.stream().filter(p -> collect.contains(p.getId())).collect(Collectors.toList());


                if (CollUtil.isNotEmpty(teachingClasses)) {
                    for (ClassInfo teachingClass : collect1) {
                        ClassInform classInform = new ClassInform();
                        classInform.setClassId(teachingClass.getId());
                        classInform.setClassName(teachingClass.getClassName());

                        List<TeachingClass> collect2 = teachingClasses.stream().filter(p -> Long.valueOf(p.getClassId()).equals(teachingClass.getId())).distinct().collect(Collectors.toList());

                        Integer state = collect2.stream().mapToInt(TeachingClass::getState).sum() == (collect2.size()) ? 1 : 0;
                        Boolean ret = collect2.stream().anyMatch(o -> o.getScheduleState() != -1);

                        classInform.setTypeName("行政班");
                        classInform.setState(state);
                        classInform.setScheduleState(ret);
                        classInformList.add(classInform);
                    }

                }

                List<ClassInform> collect2 = classInformList.stream().sorted(Comparator.comparing(ClassInform::getClassName)).collect(Collectors.toList());
                gradeClassInformation.setClassInformList(collect2);
                gradeClassInformation.setClassNum(classInformList.size());
                gradeClassInformation.setScheduleState(classInformList.stream().allMatch(ClassInform::getScheduleState));

                int state = classInformList.stream().mapToInt(ClassInform::getState).sum() == classInformList.size() ? 1 : 0;
                gradeClassInformation.setState(state);
                gradeClassInformations.add(gradeClassInformation);
            });

            int count = 0;
            for (GradeClassInformation gradeClassInformation : gradeClassInformations) {
                count = count + gradeClassInformation.getClassNum();
            }

            int classState = gradeClassInformations.stream().mapToInt(GradeClassInformation::getState).sum() == gradeClassInformations.size() ? 1 : 0;
            List<GradeClassInformation> collect = gradeClassInformations.stream().sorted(Comparator.comparing(GradeClassInformation::getGradeName)).collect(Collectors.toList());

            teachingClassTreeClassInfo.setState(classState);
            teachingClassTreeClassInfo.setGradeClassInformationList(collect);
            teachingClassTreeClassInfo.setTotalClassNum(count);
            teachingClassTreeClassInfo.setScheduleState(gradeClassInformations.stream().allMatch(GradeClassInformation::getScheduleState));
            teachingClassTreeClassInfo.setTypeName("行政班");


            //选课教学班对应班级
            gradeMap.forEach((key, value) -> {
                GradeClassInformation selectGradeInfo = new GradeClassInformation();
                selectGradeInfo.setGradeId(key);
                selectGradeInfo.setGradeName(value);
                selectGradeInfo.setTypeName("选课教学班");

                List<ClassInform> selectClassInfos = new ArrayList<>();
                List<TeachingClass> teachingClasses = finalTeachingClassList.stream().filter(p -> p.getGradeId().equals(key)
                        && StringUtils.isBlank(p.getUniqueShiftSign()) && p.getTeachingClassType().equals(TeachingClassType.SELECT.getCode()))
                        .collect(Collectors.toList());

                if (CollUtil.isNotEmpty(teachingClasses)) {
                    for (TeachingClass teachingClass : teachingClasses) {
                        ClassInform classInform = new ClassInform();
                        classInform.setGradeId(teachingClass.getGradeId());
                        classInform.setTypeName("选课教学班");
                        classInform.setGradeName(teachingClass.getGradeName());
                        classInform.setTeachingClassId(teachingClass.getTeachingClassId());
                        classInform.setTeachingClassName(teachingClass.getTeachingClassName());
                        classInform.setState(teachingClass.getState());
                        classInform.setScheduleState(teachingClass.getScheduleState() != -1);
                        selectClassInfos.add(classInform);
                    }

                }

                int state = selectClassInfos.stream().mapToInt(ClassInform::getState).sum() == selectClassInfos.size() ? 1 : 0;
                selectGradeInfo.setState(state);
                selectGradeInfo.setClassInformList(selectClassInfos);
                selectGradeInfo.setClassNum(selectClassInfos.size());
                selectGradeInfo.setScheduleState(selectClassInfos.stream().allMatch(ClassInform::getScheduleState));
                selectGradeInfos.add(selectGradeInfo);
            });


            int count1 = 0;
            for (GradeClassInformation selectGradeInfo : selectGradeInfos) {
                count1 = count1 + selectGradeInfo.getClassNum();
            }


            int selectState = selectGradeInfos.stream().mapToInt(GradeClassInformation::getState).sum() == selectGradeInfos.size() ? 1 : 0;
            List<GradeClassInformation> collect1 = selectGradeInfos.stream().sorted(Comparator.comparing(GradeClassInformation::getGradeName)).collect(Collectors.toList());
            teachingClassTreeSelectInformation.setState(selectState);
            teachingClassTreeSelectInformation.setGradeClassInformationList(collect1);
            teachingClassTreeSelectInformation.setTotalClassNum(count1);
            teachingClassTreeSelectInformation.setScheduleState(selectGradeInfos.stream().allMatch(GradeClassInformation::getScheduleState));
            teachingClassTreeSelectInformation.setTypeName("选课教学班");


            //拆班对应班级
            gradeMap.forEach((key, value) -> {
                GradeClassInformation subGradeInfo = new GradeClassInformation();
                subGradeInfo.setGradeId(key);
                subGradeInfo.setGradeName(value);
                subGradeInfo.setTypeName("拆班教学班");
                List<ClassInform> subClassInfos = new ArrayList<>();
                List<TeachingClass> teachingClasses = finalTeachingClassList.stream().filter(p -> p.getGradeId().equals(key)
                        && StringUtils.isNotBlank(p.getUniqueShiftSign()) && ObjUtil.isNotNull(p.getSubId()) && p.getTeachingClassType()
                        .equals(TeachingClassType.NOSELECT.getCode()))
                        .collect(Collectors.toList());

                if (CollUtil.isNotEmpty(teachingClasses)) {
                    for (TeachingClass teachingClass : teachingClasses) {
                        ClassInform subClassInfo = new ClassInform();
                        subClassInfo.setTypeName("拆班教学班");
                        subClassInfo.setGradeId(teachingClass.getGradeId());
                        subClassInfo.setGradeName(teachingClass.getGradeName());
                        subClassInfo.setTeachingClassId(teachingClass.getTeachingClassId());
                        subClassInfo.setTeachingClassName(teachingClass.getTeachingClassName());
                        subClassInfo.setClassName(teachingClass.getClassName());
                        subClassInfo.setState(teachingClass.getState());
                        subClassInfo.setScheduleState(teachingClass.getScheduleState() != -1);
                        subClassInfos.add(subClassInfo);
                    }

                }

                int state = subClassInfos.stream().mapToInt(ClassInform::getState).sum() == subClassInfos.size() ? 1 : 0;
                subGradeInfo.setState(state);
                subGradeInfo.setClassInformList(subClassInfos);
                subGradeInfo.setClassNum(subClassInfos.size());
                subGradeInfo.setScheduleState(subClassInfos.stream().allMatch(ClassInform::getScheduleState));
                subGradeInfos.add(subGradeInfo);
            });


            int count2 = 0;
            for (GradeClassInformation subGradeInfo : subGradeInfos) {
                count2 = count2 + subGradeInfo.getClassNum();
            }
            int subState = subGradeInfos.stream().mapToInt(GradeClassInformation::getState).sum() == subGradeInfos.size() ? 1 : 0;
            List<GradeClassInformation> collect2 = subGradeInfos.stream().sorted(Comparator.comparing(GradeClassInformation::getGradeName)).collect(Collectors.toList());
            teachingClassTreeSubInformation.setState(subState);
            teachingClassTreeSubInformation.setGradeClassInformationList(collect2);
            teachingClassTreeSubInformation.setTotalClassNum(count2);
            teachingClassTreeSubInformation.setScheduleState(subGradeInfos.stream().allMatch(GradeClassInformation::getScheduleState));
            teachingClassTreeSubInformation.setTypeName("拆班教学班");


            //合班教学班
            gradeMap.forEach((key, value) -> {
                GradeClassInformation mixeaGradeClassInformation = new GradeClassInformation();
                mixeaGradeClassInformation.setGradeId(key);
                mixeaGradeClassInformation.setGradeName(value);
                mixeaGradeClassInformation.setTypeName("合班教学班");

                List<ClassInform> classInformList = new ArrayList<>();
                List<TeachingClass> teachingClasses = finalTeachingClassList.stream().filter(p -> p.getGradeId().equals(key)
                        && StringUtils.isNotBlank(p.getUniqueShiftSign()) && ObjUtil.isNull(p.getSubId()))
                        .collect(Collectors.toList());


                if (CollUtil.isNotEmpty(teachingClasses)) {
                    for (TeachingClass teachingClass : teachingClasses) {
                        ClassInform classInform = new ClassInform();
                        classInform.setTypeName("合班教学班");
                        classInform.setGradeId(teachingClass.getGradeId());
                        classInform.setGradeName(teachingClass.getGradeName());
                        classInform.setClassId(teachingClass.getTeachingClassId());
                        classInform.setClassName(teachingClass.getTeachingClassName());
                        classInform.setTeachingClassId(teachingClass.getTeachingClassId());
                        classInform.setTeachingClassName(teachingClass.getTeachingClassName());
                        classInform.setState(teachingClass.getState());
                        classInform.setScheduleState(teachingClass.getScheduleState() != -1);
                        classInformList.add(classInform);
                    }

                }

                int state = classInformList.stream().mapToInt(ClassInform::getState).sum() == classInformList.size() ? 1 : 0;
                mixeaGradeClassInformation.setState(state);
                mixeaGradeClassInformation.setClassInformList(classInformList);
                mixeaGradeClassInformation.setClassNum(classInformList.size());
                mixeaGradeClassInformation.setScheduleState(classInformList.stream().allMatch(ClassInform::getScheduleState));
                mixedGradeInfos.add(mixeaGradeClassInformation);
            });

            int count3 = 0;
            for (GradeClassInformation mixedGradeClassInformation : mixedGradeInfos) {
                count3 = count3 + mixedGradeClassInformation.getClassNum();
            }

            int mixState = mixedGradeInfos.stream().mapToInt(GradeClassInformation::getState).sum() == mixedGradeInfos.size() ? 1 : 0;
            List<GradeClassInformation> collect3 = mixedGradeInfos.stream().sorted(Comparator.comparing(GradeClassInformation::getGradeName)).collect(Collectors.toList());
            teachingClassTreeMixedClassInfo.setState(mixState);
            teachingClassTreeMixedClassInfo.setGradeClassInformationList(collect3);
            teachingClassTreeMixedClassInfo.setTotalClassNum(count3);
            teachingClassTreeMixedClassInfo.setScheduleState(mixedGradeInfos.stream().allMatch(GradeClassInformation::getScheduleState));
            teachingClassTreeMixedClassInfo.setTypeName("合班教学班");


            teachingClassTreeList.add(teachingClassTreeClassInfo);
            teachingClassTreeList.add(teachingClassTreeSelectInformation);
            teachingClassTreeList.add(teachingClassTreeSubInformation);
            teachingClassTreeList.add(teachingClassTreeMixedClassInfo);
        }

        return teachingClassTreeList;
    }


    /**
     * 获取行政班对应的教学班列表
     *
     * @param taskId
     * @param typeId
     * @param courseId
     * @param isEnable
     * @return
     */
    @Override
    public TeachingClassByClass getClassInfoList(Integer taskId, String gradeId, Integer typeId, Long courseId, Long classId, Integer isEnable) {

        /**
         * 获取不选课不拆班行政班对应的
         */
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(StringUtils.isNotBlank(gradeId), TeachingClass::getGradeId, gradeId)
                .eq(ObjUtil.isNotEmpty(typeId), TeachingClass::getFlag, typeId)
                .eq(ObjUtil.isNotEmpty(courseId), TeachingClass::getCourseId, courseId)
                .eq(ObjUtil.isNotEmpty(isEnable), TeachingClass::getIsEnable, isEnable)
                .eq(ObjUtil.isNotEmpty(classId), TeachingClass::getClassId, String.valueOf(classId))
                .isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId)
                .distinct().collect(Collectors.toList());

        if (CollUtil.isEmpty(teachingClassIds)) {

            TeachingClassByClass teachingClassByClass = new TeachingClassByClass();

            teachingClassByClass.setClassNum(0);
            teachingClassByClass.setUnable(0);
            teachingClassByClass.setEnable(0);
            teachingClassByClass.setClassInforList(new ArrayList<>());

            return teachingClassByClass;
        }

        List<TeachingClassWeek> teachingClassWeeks = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassTeacher> teachingClassTeachers = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


        TeachingClassByClass teachingClassByClass = new TeachingClassByClass();

        List<ClassInfo> classInfoList = classInfoMapper.selectList(new LambdaQueryWrapper<ClassInfo>().eq(ClassInfo::getTaskId, taskId));

        List<ClassInfor> classInforList = new ArrayList<>();


        //合班将教学计划制空
        Map<String, List<TeachingClass>> classMap = teachingClassList.stream().sorted(Comparator.comparing(TeachingClass::getClassName)).collect(Collectors.groupingBy(TeachingClass::getClassId));


        classMap.forEach((key, value) -> {
            ClassInfor classInfor = new ClassInfor();
            classInfor.setClassId(Long.valueOf(key));

            ClassInfo classInfo = classInfoList.stream().filter(p -> p.getId().equals(Long.valueOf(key))).findFirst().orElse(null);
            if (classInfo != null) {
                classInfor.setClassNumber(classInfo.getStudentNumber());
                classInfor.setGradeId(classInfo.getGradeId());
                classInfor.setGradeName(classInfo.getGradeName());
                classInfor.setClassNumber(classInfo.getStudentNumber());
//                classInfor.setMajorId(classInfo.getMajorId());
                classInfor.setMajorName(classInfo.getMajorName());
            }
            List<CourseInformation> courseInformationList = new ArrayList<>();
            for (TeachingClass teachingClass : value) {
                if (teachingClass.getFlag() == 1) {

                    classInfor.setClassName(teachingClass.getClassName());
                    CourseInformation courseInformation = new CourseInformation();
                    courseInformation.setTeachingClassId(teachingClass.getTeachingClassId());
                    courseInformation.setTeachingClassName(teachingClass.getTeachingClassName());
                    courseInformation.setCourseId(teachingClass.getCourseId());
                    courseInformation.setCourseName(teachingClass.getCourseName());
                    courseInformation.setFlag(1);
                    List<TeachingClassWeek> classWeekList = teachingClassWeeks.stream().filter(p -> p.getTeachingClassId()
                            .equals(teachingClass.getTeachingClassId())).collect(Collectors.toList());

                    List<WeekDto> weekDtoList = new ArrayList<>();

                    for (TeachingClassWeek teachingClassWeek : classWeekList) {
                        WeekDto weekDto = new WeekDto();
                        weekDto.setAssessmentId(teachingClassWeek.getAssessmentId());
                        weekDto.setAssessmentName(teachingClassWeek.getAssessmentName());
                        weekDto.setHourType(teachingClassWeek.getHourType());
                        weekDto.setRoomId(teachingClassWeek.getRoomId());
                        weekDto.setRoomType(teachingClassWeek.getRoomType());
                        weekDto.setRoomTypeName(GlobalConstant.roomTypeMap.get(teachingClassWeek.getRoomType()));
                        weekDto.setWeek(teachingClassWeek.getWeek());
//                        weekDto.setStartWeek(teachingClassWeek.getWeek());
//                        weekDto.setEndWeek(teachingClassWeek.getWeek());
                        weekDto.setConnectSection(teachingClassWeek.getConnectSection());
                        weekDto.setConnectNumber(teachingClassWeek.getConnectNumber());

                        Integer weekNum = getOneNumber(teachingClassWeek.getWeek());
                        weekDto.setTotalStudyHour(weekNum * teachingClassWeek.getWeekNum());
                        weekDto.setWeekNum(teachingClassWeek.getWeekNum());

                        List<TeachingClassTeacher> teacher = teachingClassTeachers.stream().filter(p -> p.getTeachingClassId()
                                .equals(teachingClass.getTeachingClassId())).collect(Collectors.toList());

                        String collect = teacher.stream().map(TeachingClassTeacher::getTeacherId).distinct().map(String::valueOf).collect(Collectors.joining("、"));
                        String collect1 = teacher.stream().map(TeachingClassTeacher::getTeacherName).distinct().collect(Collectors.joining("、"));

                        weekDto.setTeacherId(collect);
                        weekDto.setTeacherName(collect1);
                        weekDtoList.add(weekDto);
                    }


                    courseInformation.setIsEnable(teachingClass.getIsEnable());
                    courseInformation.setWeekDtoList(weekDtoList);

                    courseInformationList.add(courseInformation);


                } else if (teachingClass.getFlag() == 2) {

                    CourseInformation courseInformation = new CourseInformation();
                    classInfor.setClassName(teachingClass.getClassName());
                    courseInformation.setTeachingClassId(teachingClass.getTeachingClassId());
                    courseInformation.setTeachingClassName(teachingClass.getTeachingClassName());
                    courseInformation.setCourseId(teachingClass.getCourseId());
                    courseInformation.setCourseName(teachingClass.getCourseName());
                    courseInformation.setFlag(2);
                    List<TeachingClassWeek> classWeekList = teachingClassWeeks.stream().filter(p -> p.getTeachingClassId()
                            .equals(teachingClass.getTeachingClassId())).collect(Collectors.toList());

                    List<WeekDto> weekDtoList = new ArrayList<>();

                    for (TeachingClassWeek teachingClassWeek : classWeekList) {
                        WeekDto weekDto = new WeekDto();
                        List<TeachingClassTeacher> teacher = teachingClassTeachers.stream().filter(p -> p.getTeachingClassId()
                                .equals(teachingClass.getTeachingClassId())).collect(Collectors.toList());
                        String collect = teacher.stream().map(TeachingClassTeacher::getTeacherId).distinct().map(String::valueOf).collect(Collectors.joining("、"));
                        String collect1 = teacher.stream().map(TeachingClassTeacher::getTeacherName).distinct().collect(Collectors.joining("、"));

                        weekDto.setTeacherId(collect);
                        weekDto.setTeacherName(collect1);
                        weekDto.setWeek(teachingClassWeek.getWeek());
                        weekDtoList.add(weekDto);
                    }


                    courseInformation.setIsEnable(teachingClass.getIsEnable());

                    courseInformation.setWeekDtoList(weekDtoList);
                    courseInformationList.add(courseInformation);
                }
            }
            classInfor.setCourseInformationList(courseInformationList);

            int sum = value.stream().mapToInt(TeachingClass::getState).sum();
            classInfor.setEnableNum(sum == value.size() ? 1 : 0);
            classInforList.add(classInfor);
        });

        int num = classInforList.stream().mapToInt(ClassInfor::getEnableNum).sum();
        List<ClassInfor> list = classInforList.stream().sorted(Comparator.comparing(ClassInfor::getGradeName)).collect(Collectors.toList());

        teachingClassByClass.setClassInforList(list);
        teachingClassByClass.setEnable(null);
        teachingClassByClass.setUnable(classInforList.size() - num);
        teachingClassByClass.setClassInforList(classInforList);
        teachingClassByClass.setClassNum(classInforList.size());
        return teachingClassByClass;
    }

    @Override
    public TeachingClassBySelect getSelectClassList(Integer taskId, Long teachingClassId, String gradeId, Long courseId, Integer isEnable, Integer pageSize, Integer currentPage) {

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(StringUtils.isNotBlank(gradeId), TeachingClass::getGradeId, gradeId)
                .eq(ObjUtil.isNotEmpty(courseId), TeachingClass::getCourseId, courseId)
                .eq(ObjUtil.isNotEmpty(isEnable), TeachingClass::getIsEnable, isEnable)
                .eq(ObjUtil.isNotEmpty(teachingClassId), TeachingClass::getTeachingClassId, teachingClassId)
                .isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.SELECT.getCode())
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));


        Page<TeachingClassWeek> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);

        Page<TeachingClassWeek> teachingClassWeekPage = teachingClassWeekMapper.getSelectTeachingClassWeekPage(page, StringUtils.isNotBlank(gradeId) ? gradeId : null,
                ObjUtil.isNotNull(courseId) ? courseId : null, ObjUtil.isNotNull(teachingClassId) ? teachingClassId : null, isEnable, taskId);

        List<TeachingClassWeek> records = teachingClassWeekPage.getRecords();

        List<Long> teachingClassIds = records.stream().map(TeachingClassWeek::getTeachingClassId).distinct().collect(Collectors.toList());


//        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId)
//                .distinct().collect(Collectors.toList());

        List<TeachingClass> teachingClassList1 = teachingClassList.stream().filter(p -> teachingClassIds.contains(p.getTeachingClassId())).distinct().collect(Collectors.toList());

        TeachingClassBySelect teachingClassBySelect = new TeachingClassBySelect();

        List<TeachingClassInfo> teachingClassInfoList = new ArrayList<>();

        if (CollUtil.isNotEmpty(teachingClassIds)) {

//            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
//                    .in(TeachingClassWeek::getTeachingClassId, teachingClassIds).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

            List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                    .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


            teachingClassList1.forEach(o -> {

                TeachingClassInfo teachingClassInfo = new TeachingClassInfo();

                teachingClassInfo.setTeachingClassId(o.getTeachingClassId());

                teachingClassInfo.setTeachingClassName(o.getTeachingClassName());

                teachingClassInfo.setGradeId(o.getGradeId());
                teachingClassInfo.setGradeName(o.getGradeName());

                teachingClassInfo.setClassNum(o.getClassNum());


                teachingClassInfo.setCourseId(o.getCourseId());

                teachingClassInfo.setCourseName(o.getCourseName());

                teachingClassInfo.setEnable(o.getIsEnable());

                teachingClassInfo.setState(o.getState());

                List<TeachingClassWeek> teachingClassWeeks = records.stream()
                        .filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId())).collect(Collectors.toList());


                List<WeekDto> weekDtoList = new ArrayList<>();
                if (CollUtil.isNotEmpty(teachingClassWeeks)) {

                    for (TeachingClassWeek teachingClassWeek : teachingClassWeeks) {

                        WeekDto weekDto = new WeekDto();
                        weekDto.setAssessmentId(teachingClassWeek.getAssessmentId());
                        weekDto.setAssessmentName(teachingClassWeek.getAssessmentName());
                        weekDto.setHourType(teachingClassWeek.getHourType());
                        weekDto.setRoomId(teachingClassWeek.getRoomId());
                        weekDto.setRoomType(teachingClassWeek.getRoomType());
                        weekDto.setRoomTypeName(GlobalConstant.roomTypeMap.get(teachingClassWeek.getRoomType()));
                        weekDto.setWeek(teachingClassWeek.getWeek());
                        weekDto.setConnectSection(teachingClassWeek.getConnectSection());
                        weekDto.setConnectNumber(teachingClassWeek.getConnectNumber());

                        List<TeachingClassTeacher> teacher = teachingClassTeacherList.stream().filter(p -> p.getTeachingClassId()
                                .equals(o.getTeachingClassId()) && p.getTeacherType().equals(teachingClassWeek.getHourType())).collect(Collectors.toList());

                        if (CollUtil.isNotEmpty(teacher)) {
                            String collect = teacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).collect(Collectors.joining("、"));
                            String collect1 = teacher.stream().map(TeachingClassTeacher::getTeacherName).collect(Collectors.joining("、"));
                            weekDto.setTeacherId(collect);
                            weekDto.setTeacherName(collect1);
                        }

                        weekDto.setSign(teachingClassWeek.getSign());

                        weekDto.setStartWeek(teachingClassWeek.getStartWeek());
                        weekDto.setEndWeek(teachingClassWeek.getEndWeek());

                        Integer weekNum = getOneNumber(teachingClassWeek.getWeek());
                        weekDto.setTotalStudyHour(weekNum * teachingClassWeek.getWeekNum());
                        weekDto.setWeekNum(teachingClassWeek.getWeekNum());
                        weekDtoList.add(weekDto);
                    }
                }
                List<WeekDto> collect = weekDtoList.stream().sorted(Comparator.comparing(WeekDto::getHourType)).collect(Collectors.toList());
                teachingClassInfo.setWeekDtoList(collect);

                teachingClassInfoList.add(teachingClassInfo);
            });

        }

        int sum = teachingClassList.stream().mapToInt(TeachingClass::getState).sum();


        teachingClassBySelect.setEnable(sum);

        teachingClassBySelect.setSize(records.size());

        teachingClassBySelect.setTotalSize(teachingClassWeekPage.getTotal());

        teachingClassBySelect.setUnable(teachingClassList.size() - sum);

        teachingClassBySelect.setTotalClassNum(teachingClassList.size());

        teachingClassBySelect.setTeachingClassInfoList(teachingClassInfoList);

        return teachingClassBySelect;

    }

    /**
     * 获取拆班列表
     *
     * @param taskId
     * @param teachingClassId
     * @param subType
     * @param gradeId
     * @param courseId
     * @param isEnable
     * @return
     */
    @Override
    public TeachingClassBySub getSubClassInfoList(Integer taskId, Long teachingClassId, Integer subType, String gradeId, Long courseId, Integer isEnable, Integer pageSize, Integer currentPage) {
        TeachingClassBySub teachingClassBySub = new TeachingClassBySub();

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(StringUtils.isNotBlank(gradeId), TeachingClass::getGradeId, gradeId)
                .eq(ObjUtil.isNotEmpty(subType), TeachingClass::getSubId, subType)
                .eq(ObjUtil.isNotEmpty(courseId), TeachingClass::getCourseId, courseId)
                .eq(ObjUtil.isNotEmpty(isEnable), TeachingClass::getIsEnable, isEnable)
                .eq(ObjUtil.isNotEmpty(teachingClassId), TeachingClass::getTeachingClassId, teachingClassId)
                .isNotNull(TeachingClass::getUniqueShiftSign)
                .isNotNull(TeachingClass::getSubId)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));


        Page<TeachingClassWeek> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);

        Page<TeachingClassWeek> teachingClassWeekPage = teachingClassWeekMapper.getSubTeachingClassWeekPage(page, StringUtils.isNotBlank(gradeId) ? gradeId : null
                , ObjUtil.isNotEmpty(subType) ? subType : null,
                ObjUtil.isNotEmpty(courseId) ? courseId : null, ObjUtil.isNotEmpty(teachingClassId) ? teachingClassId : null, isEnable, taskId);


        List<TeachingClassWeek> records = teachingClassWeekPage.getRecords();

        List<Long> teachingClassIds = records.stream().map(TeachingClassWeek::getTeachingClassId).distinct().collect(Collectors.toList());

        List<TeachingClass> teachingClassList1 = teachingClassList.stream().filter(p -> teachingClassIds.contains(p.getTeachingClassId())).distinct().collect(Collectors.toList());


        TaskInfo taskInfo = taskInfoService.getById(taskId);
        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>().eq(Room::getSchoolId, taskInfo.getSchoolId()).eq(Room::getTerm, taskInfo.getTerm()));

        if (CollUtil.isNotEmpty(teachingClassIds)) {

            List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                    .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


            List<TeachingClassSubInfo> teachingClassSubInfos = new ArrayList<>();


            teachingClassList1.forEach(o -> {

                TeachingClassSubInfo teachingClassSubInfo = new TeachingClassSubInfo();

                teachingClassSubInfo.setTeachingClassId(o.getTeachingClassId());

                teachingClassSubInfo.setTeachingClassName(o.getTeachingClassName());

                teachingClassSubInfo.setGradeId(o.getGradeId());
                teachingClassSubInfo.setGradeName(o.getGradeName());


                teachingClassSubInfo.setCourseId(o.getCourseId());

                teachingClassSubInfo.setCourseName(o.getCourseName());

                teachingClassSubInfo.setSubId(o.getSubId());

                teachingClassSubInfo.setSubName(o.getSubName());

                teachingClassSubInfo.setClassNum(o.getClassNum());

                teachingClassSubInfo.setEnable(o.getIsEnable());

                teachingClassSubInfo.setState(o.getState());


                List<TeachingClassWeek> teachingClassWeeks = records.stream()
                        .filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId())).collect(Collectors.toList());


                List<WeekDto> weekDtoList = new ArrayList<>();
                if (CollUtil.isNotEmpty(teachingClassWeeks)) {

                    for (TeachingClassWeek teachingClassWeek : teachingClassWeeks) {

                        WeekDto weekDto = new WeekDto();
                        weekDto.setAssessmentId(teachingClassWeek.getAssessmentId());
                        weekDto.setAssessmentName(teachingClassWeek.getAssessmentName());
                        weekDto.setHourType(teachingClassWeek.getHourType());
                        weekDto.setRoomId(teachingClassWeek.getRoomId());
                        if (ObjUtil.isNotNull(teachingClassWeek.getRoomId())) {

                            Room room = roomList.stream().filter(p -> p.getRoomId().equals(teachingClassWeek.getRoomId())).findFirst().orElse(null);
                            if (room != null) {
                                weekDto.setRoomName(room.getRoomName());
                            }
                        }
                        weekDto.setRoomType(teachingClassWeek.getRoomType());
                        weekDto.setRoomTypeName(GlobalConstant.roomTypeMap.get(teachingClassWeek.getRoomType()));
                        weekDto.setWeek(teachingClassWeek.getWeek());
                        weekDto.setConnectSection(teachingClassWeek.getConnectSection());
                        weekDto.setConnectNumber(teachingClassWeek.getConnectNumber());

                        List<TeachingClassTeacher> teacher = teachingClassTeacherList.stream().filter(p -> p.getTeachingClassId()
                                .equals(o.getTeachingClassId()) && p.getTeacherType().equals(teachingClassWeek.getHourType())).collect(Collectors.toList());

                        if (CollUtil.isNotEmpty(teacher)) {
                            String collect = teacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).collect(Collectors.joining("、"));
                            String collect1 = teacher.stream().map(TeachingClassTeacher::getTeacherName).collect(Collectors.joining("、"));
                            weekDto.setTeacherId(collect);
                            weekDto.setTeacherName(collect1);
                        }


                        weekDto.setSign(teachingClassWeek.getSign());
                        weekDto.setStartWeek(teachingClassWeek.getStartWeek());
                        weekDto.setEndWeek(teachingClassWeek.getEndWeek());


                        Integer weekNum = getOneNumber(teachingClassWeek.getWeek());
                        weekDto.setTotalStudyHour(weekNum * teachingClassWeek.getWeekNum());

                        weekDto.setWeekNum(teachingClassWeek.getWeekNum());
                        weekDtoList.add(weekDto);

                    }
                }


                teachingClassSubInfo.setWeekDtoList(weekDtoList);

                teachingClassSubInfos.add(teachingClassSubInfo);
            });

            int sum = teachingClassList.stream().mapToInt(TeachingClass::getState).sum();
            List<TeachingClassSubInfo> collect = new ArrayList<>();
            List<TeachingClassSubInfo> collect1 = teachingClassSubInfos.stream()
                    .filter(p -> p.getSubId().equals(1)).sorted(Comparator.comparing(TeachingClassSubInfo::getGradeName))
                    .sorted(Comparator.comparing(TeachingClassSubInfo::getCourseName)).collect(Collectors.toList());
            List<TeachingClassSubInfo> collect2 = teachingClassSubInfos.stream()
                    .filter(p -> p.getSubId().equals(0)).sorted(Comparator.comparing(TeachingClassSubInfo::getGradeName))
                    .sorted(Comparator.comparing(TeachingClassSubInfo::getCourseName)).collect(Collectors.toList());
            collect.addAll(collect1);
            collect.addAll(collect2);

            teachingClassBySub.setEnable(sum);

            teachingClassBySub.setSize(records.size());

            teachingClassBySub.setTotalSize(teachingClassWeekPage.getTotal());

            teachingClassBySub.setUnable(teachingClassList.size() - sum);

            teachingClassBySub.setTotalClassNum(teachingClassList.size());

            teachingClassBySub.setTeachingClassSubInfoList(collect);

        }


        return teachingClassBySub;
    }


    /**
     * 获取行政班对应的班级列表
     *
     * @param taskId
     * @param courseId
     * @return
     */
    @Override
    public List<SubGradeDto> getRegularSubClassList(Integer taskId, Long courseId) {


        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getCourseId, courseId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .isNull(TeachingClass::getUniqueShiftSign));

        List<SubGradeDto> subGradeDtos = new ArrayList<>();

        Map<String, List<TeachingClass>> gradeMap = teachingClassList.stream().collect(Collectors.groupingBy(TeachingClass::getGradeId));


        gradeMap.forEach((key, value) -> {
            SubGradeDto subGradeDto = new SubGradeDto();
            TeachingClass teachingClass = value.get(0);
            subGradeDto.setGradeId(key);
            subGradeDto.setGradeName(teachingClass.getGradeName());
            List<TeachingClass> collect = value.stream().sorted(Comparator.comparing(TeachingClass::getClassName))
                    .collect(Collectors.toList());
            subGradeDto.setTeachingClassList(collect);
            subGradeDtos.add(subGradeDto);
        });

        return subGradeDtos;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addRegularSubClass(Integer taskId, String uid, AddRegularSubDto addRegularSubDto) {


        List<TeachingClassStudent> teachingClassStudentList = new ArrayList<>();


        //删除之前的记录
        if (StringUtils.isNotBlank(uid)) {

            List<TeachingClass> deleteTeachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                    .eq(TeachingClass::getUniqueShiftSign, uid).eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));


            List<Long> teachingClassIds = deleteTeachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());


            if (CollUtil.isNotEmpty(teachingClassIds)) {
                //删除数据
                teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                        .eq(TeachingClass::getTaskId, taskId)
                        .in(TeachingClass::getTeachingClassId, teachingClassIds)
                        .set(TeachingClass::getDeleted, Deleted.YES.getCode()));


                teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>().
                        in(TeachingClassWeek::getTeachingClassId, teachingClassIds).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));

                teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                        .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));

                teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                        .in(TeachingClassStudent::getTeachingClassId, teachingClassIds).set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));
            }


            //恢复数据
            if(!CollUtil.isEmpty(deleteTeachingClassList)){

                List<String> collect = deleteTeachingClassList.stream().map(TeachingClass::getClassId).distinct().collect(Collectors.toList());
                List<String> classIds = new ArrayList<>();
                collect.forEach(o -> {
                    ArrayList<String> strings = new ArrayList<>(Arrays.asList(o.split(",")));
                    classIds.addAll(strings);
                });

                List<String> collect1 = classIds.stream().distinct().collect(Collectors.toList());

                List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                        .in(TeachingClass::getClassId, collect1).eq(TeachingClass::getCourseId,addRegularSubDto.getCourseId())
                        .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode()).isNull(TeachingClass::getUniqueShiftSign));

                List<Long> collect2 = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

                if (CollUtil.isNotEmpty(collect2)) {
                    teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                            .eq(TeachingClass::getTaskId, taskId)
                            .in(TeachingClass::getTeachingClassId, collect2)
                            .set(TeachingClass::getDeleted, Deleted.NOT.getCode()));


                    teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>().
                            in(TeachingClassWeek::getTeachingClassId, collect2)
                            .set(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

                    teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                            .in(TeachingClassTeacher::getTeachingClassId, collect2)
                            .set(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

                    teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                            .in(TeachingClassStudent::getTeachingClassId, collect2).set(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));

                }
            }


        }


        //进行混合拆班

        Integer teachingClassNum = addRegularSubDto.getClassNum();

        List<Long> teachingClassIds = addRegularSubDto.getTeachingIds();

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, teachingClassIds));


        List<TeachingClassStudent> teachingClassStudents = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>().in(TeachingClassStudent::getTeachingClassId, teachingClassIds));


        List<Student> students = studentMapper.selectList(new LambdaQueryWrapper<Student>().eq(Student::getTaskId, taskId).eq(Student::getDeleted, Deleted.NOT.getCode()));


        List<Student> studentList = students.stream().filter(p -> teachingClassStudents.stream().anyMatch(o -> o.getStudentId().equals(p.getStudentId()))).collect(Collectors.toList());


        List<String> classIds = teachingClassList.stream().map(TeachingClass::getClassId).distinct().collect(Collectors.toList());

        List<String> className = teachingClassList.stream().map(TeachingClass::getClassName).distinct().collect(Collectors.toList());


        TeachingClass teachingClass = new TeachingClass();
        if (CollUtil.isNotEmpty(teachingClassList)) {
            teachingClass = teachingClassList.get(0);
        }

        List<TeachingClass> teachingClasses = new ArrayList<>();

        List<Integer> classNum = teachingClassList.stream().map(TeachingClass::getClassNum).collect(Collectors.toList());
        Integer totalNum = 0;

        for (Integer num : classNum) {
            totalNum = totalNum + num;
        }


        UUID uuid = UUID.randomUUID();
        for (int i = 0; i < teachingClassNum; i++) {
            TeachingClass tclass = new TeachingClass();
            tclass.setTaskId(taskId);
            tclass.setTeachingClassName(teachingClass.getGradeName() + teachingClass.getCourseName() + "常规拆班" + (i + 1) + "班");
            tclass.setClassId(String.join(",", classIds));
            tclass.setClassName(String.join(",", className));
            tclass.setTeachingPlanId(teachingClass.getTeachingPlanId());

            tclass.setCourseId(teachingClass.getCourseId());
            tclass.setCourseName(teachingClass.getCourseName());
            tclass.setCampusId(teachingClass.getCampusId());
            tclass.setCampusName(teachingClass.getCampusName());
            if (totalNum % teachingClassNum == 0) {
                tclass.setClassNum(totalNum / teachingClassNum);
            } else {
                tclass.setClassNum((i < (totalNum % teachingClassNum)) ? ((totalNum / teachingClassNum) + 1) : (totalNum / teachingClassNum));
            }

            tclass.setGradeId(teachingClass.getGradeId());
            tclass.setSubId(SubType.GENERAL.getCode());
            tclass.setSubName(SubType.GENERAL.getMsg());
            tclass.setUniqueShiftSign(uuid.toString());
            tclass.setGradeName(teachingClass.getGradeName());
            tclass.setTeachingClassType(TeachingClassType.NOSELECT.getCode());
            tclass.setFlag(1);
            tclass.setCampusId(teachingClass.getCampusId());
            tclass.setCampusName(teachingClass.getCampusName());
            tclass.setIsEnable(teachingClass.getIsEnable());
            teachingClasses.add(tclass);

        }

        teachingClassService.saveBatch(teachingClasses);


        // 添加teachingClassWeek

        List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClass.getTeachingClassId())
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));
        List<TeachingClassWeek> teachingClassWeeks = new ArrayList<>();

        for (TeachingClass subClass : teachingClasses) {
            for (TeachingClassWeek teachingClassWeek : teachingClassWeekList) {
                TeachingClassWeek tClassWeek = new TeachingClassWeek();
                tClassWeek.setTeachingClassId(subClass.getTeachingClassId());
                tClassWeek.setWeek(teachingClassWeek.getWeek());
                tClassWeek.setWeekNum(teachingClassWeek.getWeekNum());
                tClassWeek.setSign(teachingClassWeek.getSign());
                tClassWeek.setStartWeek(teachingClassWeek.getStartWeek());
                tClassWeek.setEndWeek(teachingClassWeek.getEndWeek());
                tClassWeek.setConnectSection(teachingClassWeek.getConnectSection());
                tClassWeek.setConnectNumber(teachingClassWeek.getConnectNumber());
                tClassWeek.setAssessmentId(teachingClassWeek.getAssessmentId());
                tClassWeek.setAssessmentName(teachingClassWeek.getAssessmentName());
                tClassWeek.setHourType(teachingClassWeek.getHourType());
                teachingClassWeeks.add(tClassWeek);
            }
        }

        teachingClassWeekService.saveBatch(teachingClassWeeks);


        int i = 0;
        for (Student student : studentList) {

            TeachingClass teachingClass1 = teachingClasses.get(i % teachingClassNum);

            TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
            teachingClassStudent.setTeachingClassId(teachingClass1.getTeachingClassId());
            teachingClassStudent.setStudentId(student.getStudentId());
            teachingClassStudentList.add(teachingClassStudent);
            i++;
        }


        teachingClassStudentService.saveBatch(teachingClassStudentList);


        teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().
                eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, teachingClassIds)
                .set(TeachingClass::getDeleted, Deleted.YES.getCode()));

        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));

        teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));

        teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>().in(TeachingClassStudent::getTeachingClassId, teachingClassIds)
                .set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));

        return uuid.toString();
    }

    @Override
    public List<TeachingClass> updateRegularSubClassList(Integer taskId, String uuid) {
        return teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getUniqueShiftSign, uuid));
    }

    /**
     * 获取单双号拆班列表
     *
     * @param taskId
     * @param subId
     * @return
     */
    @Override
    public AddOddEvenDto getOddEvenSubClass(Integer taskId, String subId) {
        AddOddEvenDto addOddEvenDto = new AddOddEvenDto();

        List<SubTeachingClassDto> subTeachingClassDtos = new ArrayList<>();


        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getUniqueShiftSign, subId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teachingIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).collect(Collectors.toList());

        List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingIds)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, teachingIds)
                .eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


        TaskInfo taskInfo = taskInfoService.getById(taskId);

        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>().
                eq(Room::getSchoolId, taskInfo.getSchoolId()).eq(Room::getTerm, taskInfo.getTerm())
                .eq(Room::getDeleted, Deleted.NOT.getCode()));

        Map<Long, List<TeachingClass>> courseMap = teachingClassList.stream().collect(Collectors.groupingBy(TeachingClass::getCourseId));


        courseMap.forEach((key, value) -> {
            SubTeachingClassDto subTeachingClassDto = new SubTeachingClassDto();

            subTeachingClassDto.setCourseId(key);

            List<SubTeachingClassAdd> subTeachingClassAddList = new ArrayList<>();
            value.forEach(o -> {
                subTeachingClassDto.setCourseName(o.getCourseName());
                SubTeachingClassAdd subTeachingClassAdd = new SubTeachingClassAdd();
                subTeachingClassAdd.setTeachingClassId(o.getTeachingClassId());
                subTeachingClassAdd.setClassId(o.getClassId());
                subTeachingClassAdd.setClassName(o.getTeachingClassName());


                subTeachingClassAdd.setFlag(o.getIsSingle());
                subTeachingClassAdd.setCampusId(o.getCampusId());
                subTeachingClassAdd.setCampusName(o.getCampusName());
                List<TeachingClassWeek> teachingClassWeek = teachingClassWeekList.stream().filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId())).collect(Collectors.toList());

                if (CollUtil.isNotEmpty(teachingClassWeek)) {

                    TeachingClassWeek teachingClassWeek1 = teachingClassWeek.stream().filter(p -> p.getHourType().equals(1)).findFirst().orElse(null);

                    if (ObjUtil.isNotNull(teachingClassWeek1.getRoomId())) {

                        List<Room> rooms = roomList.stream().filter(p -> p.getRoomId().equals(teachingClassWeek1.getRoomId())).collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(rooms)) {
                            Room room = rooms.get(0);
                            subTeachingClassAdd.setRoomId(room.getRoomId());
                            subTeachingClassAdd.setRoomName(room.getRoomName());
                        }
                    }
                    subTeachingClassAdd.setWeekNum(teachingClassWeek1.getWeekNum());
                }
                List<TeachingClassTeacher> teachingClassTeacher = teachingClassTeacherList.stream().filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId())).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(teachingClassTeacher)) {
                    String collect = teachingClassTeacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).distinct().collect(Collectors.joining("、"));
                    String collect1 = teachingClassTeacher.stream().map(TeachingClassTeacher::getTeacherName).distinct().collect(Collectors.joining("、"));
                    subTeachingClassAdd.setTeacherId(collect);
                    subTeachingClassAdd.setTeacherName(collect1);
                }

                subTeachingClassAddList.add(subTeachingClassAdd);
            });

            subTeachingClassDto.setTeachingClassList(subTeachingClassAddList);

            subTeachingClassDtos.add(subTeachingClassDto);
        });

        addOddEvenDto.setSubId(subId);
        addOddEvenDto.setSubTeachingClassDtos(subTeachingClassDtos);

        return addOddEvenDto;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateOddEvenSubClass(Integer taskId, String subId, List<Long> teachingClassId, Long teacherId, String teacherName, Long roomId) {


        List<TeachingClassWeek> teachingClassWeek = teachingClassWeekMapper
                .selectList(new LambdaQueryWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, teachingClassId));

        if (ObjUtil.isNotNull(roomId)) {
            List<TeachingClass> teachingClass = teachingClassMapper
                    .selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                            .in(TeachingClass::getTeachingClassId, teachingClassId));

            teachingClass.forEach(o -> {
                o.setRoomId(roomId);
            });

            TaskInfo taskInfo = taskInfoService.getById(taskId);
            Room room = roomMapper.selectOne(new LambdaQueryWrapper<Room>().eq(Room::getSchoolId, taskInfo.getSchoolId())
                    .eq(Room::getTerm, taskInfo.getTerm()).eq(Room::getRoomId, roomId));

            teachingClassWeek.forEach(o -> {
                o.setRoomType(room.getRoomType());
                o.setRoomId(roomId);
            });

            teachingClassService.saveOrUpdateBatch(teachingClass);
            teachingClassWeekService.saveOrUpdateBatch(teachingClassWeek);
        }


        if (ObjUtil.isNotNull(teacherId) && StringUtils.isNotBlank(teacherName)) {


            teachingClassTeacherMapper.delete(new LambdaQueryWrapper<TeachingClassTeacher>().in(TeachingClassTeacher::getTeachingClassId, teachingClassId));

            List<TeachingClassTeacher> teachingClassTeacherList = new ArrayList<>();

            teachingClassWeek.forEach(o -> {
                teachingClassId.forEach(m -> {
                    if (o.getTeachingClassId().equals(m)) {
                        TeachingClassTeacher teachingClassTeacher = new TeachingClassTeacher();
                        teachingClassTeacher.setTeachingClassId(m);
                        teachingClassTeacher.setTeacherId(teacherId);
                        teachingClassTeacher.setTeacherName(teacherName);
                        teachingClassTeacher.setTeacherType(o.getHourType());
                        teachingClassTeacherList.add(teachingClassTeacher);
                    }
                });
            });

            teachingClassTeacherService.saveBatch(teachingClassTeacherList);

            //同步教学班名称重复、
            List<TeachingClass> teachingClassList = teachingClassService.lambdaQuery()
                    .eq(TeachingClass::getUniqueShiftSign, subId)
                    .eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                    .list();
            long count = teachingClassList.stream().map(TeachingClass::getCourseId).distinct().count();
            if (count == 1) {
                Teacher teacher = teacherService.lambdaQuery().eq(Teacher::getTeacherId, teacherId).one();
                teachingClassList = teachingClassList.stream().filter(o -> teachingClassId.contains(o.getTeachingClassId())).collect(Collectors.toList());
                teachingClassList.forEach(o -> {
                    o.setState(1);
                    String name = o.getTeachingClassName().split("-")[0];
                    o.setTeachingClassName(name + "-" + teacher.getBaseTeacherId());
//                    o.setTeachingClassName(name + "-" + teacherName);
                });
                teachingClassService.saveOrUpdateBatch(teachingClassList);
            } else {
                teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                        .eq(TeachingClass::getTaskId, taskId)
                        .in(TeachingClass::getTeachingClassId, teachingClassId)
                        .set(TeachingClass::getState, 1));
            }

//            teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, teachingClassId).set(TeachingClass::getState, 1));
        }


        return "单双号拆班修改完成";
    }

    @Transactional
    @Override
    public Result deletedOddEvenSubClass(Integer taskId, String subId, List<DeletedOddEvenDto> data) {

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getUniqueShiftSign, subId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> allTeachingIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

        List<Long> deletedTeachingClassIds = data.stream().map(DeletedOddEvenDto::getTeachingClassId).distinct().collect(Collectors.toList());

        allTeachingIds.removeAll(deletedTeachingClassIds);

        if (CollUtil.isNotEmpty(allTeachingIds)) {

            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, allTeachingIds)
                    .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));


            Map<Long, List<TeachingClass>> collect1 = teachingClassList.stream().filter(p -> allTeachingIds.contains(p.getTeachingClassId())).
                    collect(Collectors.toList()).stream().collect(Collectors.groupingBy(TeachingClass::getCourseId));


            if (collect1.size() == 1) {
                return Result.ofFail("剩下课程不满足 周学时最大课程的周学时，小于等于其他课程周学时的总和,请调整后删除");
            }


            int max = 0;
            int sum = 0;


            for (List<TeachingClass> value : collect1.values()) {
                List<Long> collect = value.stream().map(TeachingClass::getTeachingClassId).collect(Collectors.toList());

                List<TeachingClassWeek> collect2 = teachingClassWeekList.stream().filter(p -> collect.contains(p.getTeachingClassId())).collect(Collectors.toList());

                int sum1 = collect2.stream().mapToInt(TeachingClassWeek::getWeekNum).sum();
                sum = sum + sum1;
                max = Math.max(sum1, max);
            }
            int otherNum = sum - max;

            if (max > otherNum) {
                return Result.ofFail("剩下课程不满足 周学时最大课程的周学时，小于等于其他课程周学时的总和,请调整后删除");
            }
        }


        DeletedOddEvenDto deletedOddEvenDto = data.get(0);

        //获取之前为拆班的教学班
        TeachingClass teachingClasses = teachingClassMapper.selectOne(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getClassId, String.valueOf(deletedOddEvenDto.getClassId()))
                .eq(TeachingClass::getCourseId, deletedOddEvenDto.getCourseId()).isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .eq(TeachingClass::getIsEnable, IsEnable.ENABLE.getCode()));

        // 恢复之前的数据
        if (teachingClasses != null) {

            teachingClasses.setDeleted(Deleted.NOT.getCode());

            teachingClassMapper.updateById(teachingClasses);

            teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>().eq(TeachingClassWeek::getTeachingClassId, teachingClasses.getTeachingClassId()).set(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

            teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                    .eq(TeachingClassTeacher::getTeachingClassId, teachingClasses.getTeachingClassId()).set(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

            teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                    .eq(TeachingClassStudent::getTeachingClassId, teachingClasses.getTeachingClassId()).set(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));

        }

        //删除需要删除的数据

        teachingClassService.lambdaUpdate().eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, deletedTeachingClassIds).remove();

        teachingClassWeekService.lambdaUpdate().in(TeachingClassWeek::getTeachingClassId, deletedTeachingClassIds).remove();

        teachingClassTeacherService.lambdaUpdate()
                .in(TeachingClassTeacher::getTeachingClassId, deletedTeachingClassIds).remove();

        teachingClassStudentService.lambdaUpdate()
                .in(TeachingClassStudent::getTeachingClassId, deletedTeachingClassIds).remove();


        return Result.ofSuccess("删除成功");
    }

    @Override
    public List<Course> getRegularSubCourseList(Integer taskId) {

        List<Long> teachingClasses = teachingClassMapper.selectSubCourse(taskId);

        List<Long> courseIds = teachingClasses.stream().distinct().collect(Collectors.toList());

        List<Course> courseList = courseMapper.getCourseList(taskId);
        if (CollUtil.isNotEmpty(courseIds)) {
            courseList = courseList.stream().filter(p -> courseIds.contains(p.getCourseId())).collect(Collectors.toList());
        }

        return courseList;
    }

    @Override
    public AddRegularDto getRegularSubClass(Integer taskId, Long courseId, String uuid) {

        AddRegularDto addRegularDto = new AddRegularDto();

        List<RegularDto> regularDtos = new ArrayList<>();


        List<Student> students = studentMapper.selectList(new LambdaQueryWrapper<Student>().eq(Student::getTaskId, taskId).eq(Student::getDeleted, Deleted.NOT.getCode()));


        TaskInfo taskInfo = taskInfoService.getById(taskId);


        //获取拆班后的班级
        List<TeachingClass> teachingClasses = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getUniqueShiftSign, uuid).eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teachingIds = teachingClasses.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());


        List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, teachingIds).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassStudent> teachingClassStudentList = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>()
                .in(TeachingClassStudent::getTeachingClassId, teachingIds).eq(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, teachingIds)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));


        teachingClasses.forEach(o -> {

            RegularDto regularDto = new RegularDto();

            regularDto.setTeachingClassId(o.getTeachingClassId());
            regularDto.setTeachingClassName(o.getTeachingClassName());
            List<TeachingClassTeacher> teachingClassTeacher = teachingClassTeacherList.stream().filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId())).collect(Collectors.toList());

            if (CollUtil.isNotEmpty(teachingClassTeacher)) {

                String collect = teachingClassTeacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).distinct().collect(Collectors.joining("、"));
                String collect1 = teachingClassTeacher.stream().map(TeachingClassTeacher::getTeacherName).distinct().collect(Collectors.joining("、"));
                regularDto.setTeacherId(collect);
                regularDto.setTeacherName(collect1);
            }

            List<TeachingClassWeek> teachingClassWeekList1 = teachingClassWeekList.stream()
                    .filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId()) && p.getHourType().equals(1))
                    .collect(Collectors.toList());

            if (CollUtil.isNotEmpty(teachingClassWeekList1)) {
                TeachingClassWeek teachingClassWeek = teachingClassWeekList1.get(0);
                if (ObjUtil.isNotNull(teachingClassWeek.getRoomType())) {
                    regularDto.setRoomType(GlobalConstant.roomTypeMap.get(teachingClassWeek.getRoomType()));
                    regularDto.setRoomTypeId(teachingClassWeek.getRoomType());
                }
            }

            regularDto.setCampusId(o.getCampusId());
            regularDto.setCampusName(o.getCampusName());
            regularDto.setUuid(uuid);
            List<TeachingClassStudent> teachingClassStudentList1 = teachingClassStudentList.stream().filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId())).collect(Collectors.toList());
            List<Student> students1 = students.stream().filter(p -> teachingClassStudentList1.stream().anyMatch(s -> s.getStudentId().equals(p.getStudentId()))).collect(Collectors.toList());
            regularDto.setStudentLists(students1);
            regularDtos.add(regularDto);
        });


        TeachingClass teachingClass = teachingClasses.get(0);

        String classId = teachingClass.getClassId();

        List<String> classIds = Arrays.asList(classId.split(","));

        List<TeachingClass> beforeTeaching = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                .in(TeachingClass::getClassId, classIds).eq(TeachingClass::getCourseId, courseId).isNull(TeachingClass::getUniqueShiftSign));

        addRegularDto.setRegularDtoList(regularDtos);

        addRegularDto.setTeachingClassList(beforeTeaching);


        return addRegularDto;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateRegularSubClass(Integer taskId, String subId, Long teachingClassId, Long teacherId, String teacherName, Integer roomType) {

        List<TeachingClassWeek> teachingClassWeek = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClassId));
        if (ObjUtil.isNotNull(roomType)) {
            TeachingClass teachingClass = teachingClassMapper.selectOne(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                    .eq(TeachingClass::getTeachingClassId, teachingClassId));

//            teachingClass.setRoomId(roomId);

//            Room room = roomMapper.selectOne(new LambdaQueryWrapper<Room>().eq(Room::getTaskId, taskId).eq(Room::getRoomId, roomId));

            teachingClassWeek.forEach(o -> {
                o.setRoomType(roomType);
                o.setRoomId(null);
            });

            teachingClassService.saveOrUpdate(teachingClass);
            teachingClassWeekService.saveOrUpdateBatch(teachingClassWeek);
        }

        if (ObjUtil.isNotNull(teacherId) && StringUtils.isNotBlank(teacherName)) {
            teachingClassTeacherMapper.delete(new LambdaQueryWrapper<TeachingClassTeacher>().eq(TeachingClassTeacher::getTeachingClassId, teachingClassId));


            List<TeachingClassTeacher> teachingClassTeacherList = new ArrayList<>();

            teachingClassWeek.forEach(o -> {
                TeachingClassTeacher teachingClassTeacher = new TeachingClassTeacher();
                teachingClassTeacher.setTeachingClassId(teachingClassId);
                teachingClassTeacher.setTeacherId(teacherId);
                teachingClassTeacher.setTeacherName(teacherName);
                teachingClassTeacher.setTeacherType(o.getHourType());
                teachingClassTeacherList.add(teachingClassTeacher);
            });

            teachingClassTeacherService.saveBatch(teachingClassTeacherList);

            teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getTeachingClassId, teachingClassId).set(TeachingClass::getState, 1));
        }
        return "常规拆班修改成功";
    }

    @Override
    public String deletedRegularStudent(Integer taskId, Long teachingClassId, List<Long> studentIds) {
        teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                .eq(TeachingClassStudent::getTeachingClassId, teachingClassId).in(TeachingClassStudent::getStudentId, studentIds)
                .eq(TeachingClassStudent::getDeleted, Deleted.NOT.getCode())
                .set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));
        return "学生已移除";
    }


    @Override
    public List<Student> getRegularStudent(Integer taskId, String uuid) {

        List<TeachingClass> teachingClasses = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getUniqueShiftSign, uuid)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teachingClassIds = teachingClasses.stream()
                .map(TeachingClass::getTeachingClassId)
                .distinct().collect(Collectors.toList());

        List<Student> students = studentMapper.selectList(new LambdaQueryWrapper<Student>()
                .eq(Student::getTaskId, taskId)
                .eq(Student::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassStudent> teachingClassStudentList = teachingClassStudentMapper
                .selectList(new LambdaQueryWrapper<TeachingClassStudent>()
                        .in(TeachingClassStudent::getTeachingClassId, teachingClassIds)
                        .eq(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));

        List<Student> result = students.stream().filter(p -> teachingClassStudentList.stream().anyMatch(o -> o.getStudentId().equals(p.getStudentId()))).collect(Collectors.toList());


        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addRegularStudent(Integer taskId, Long teachingClassId, List<Long> studentIds, String uuid) {
        List<TeachingClassStudent> teachingClassStudentList = new ArrayList<>();


        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getUniqueShiftSign, uuid)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());


        teachingClassStudentMapper.delete(new LambdaQueryWrapper<TeachingClassStudent>().in(TeachingClassStudent::getTeachingClassId, teachingClassIds)
                .in(TeachingClassStudent::getStudentId, studentIds));

        List<TeachingClassStudent> teachingClassStudents = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>()
                .eq(TeachingClassStudent::getTeachingClassId, teachingClassId).in(TeachingClassStudent::getStudentId, studentIds));


//        List<Long> enableStudent = teachingClassStudents.stream().map(TeachingClassStudent::getStudentId).distinct().collect(Collectors.toList());
//
//
//        studentIds.removeAll(enableStudent);

        for (Long studentDto : studentIds) {
            TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
            teachingClassStudent.setTeachingClassId(teachingClassId);
            teachingClassStudent.setStudentId(studentDto);
            teachingClassStudent.setDeleted(Deleted.NOT.getCode());
            teachingClassStudentList.add(teachingClassStudent);
        }
        teachingClassStudentService.saveOrUpdateBatch(teachingClassStudentList);

//        teachingClassStudentService.lambdaUpdate().in(TeachingClassStudent::getStudentId, enableStudent)
//                .set(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()).update();

        return "学生已经添加";
    }

    @Override
    public String deletedRegularSubClassList(Integer taskId, String uuid, Long teachingClassId, Long courseId) {
        List<TeachingClass> teachingClasses = teachingClassMapper
                .selectList(new LambdaQueryWrapper<TeachingClass>()
                        .eq(TeachingClass::getTaskId, taskId)
                        .eq(TeachingClass::getUniqueShiftSign, uuid)
                        .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClass> unSubTeachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getIsEnable, IsEnable.ENABLE.getCode())
                .eq(TeachingClass::getCourseId, courseId)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .isNull(TeachingClass::getUniqueShiftSign));


        List<Long> subTeachingClassId = teachingClasses.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());
        Integer teachingClassNum = teachingClasses.size();

        //如果数据为1 恢复之前的数据
        if (teachingClassNum.equals(1)) {
            TeachingClass teachingClass = teachingClasses.stream().filter(p -> p.getTeachingClassId().equals(teachingClassId)).findFirst().orElse(null);

            if (teachingClass != null) {
                String cs = teachingClass.getClassId();

                List<String> classIds = Arrays.asList(cs.split(","));


                List<TeachingClass> teachingClassList = unSubTeachingClassList.stream()
                        .filter(p -> classIds.stream().anyMatch(o -> o.equals(p.getClassId())))
                        .collect(Collectors.toList());

                List<Long> beforeTeachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

                teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                        .in(TeachingClass::getTeachingClassId, beforeTeachingClassIds).set(TeachingClass::getDeleted, Deleted.NOT.getCode()));
                teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                        .in(TeachingClassWeek::getTeachingClassId, beforeTeachingClassIds).set(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

                teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                        .in(TeachingClassStudent::getTeachingClassId, beforeTeachingClassIds).set(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));
                teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                        .in(TeachingClassTeacher::getTeachingClassId, beforeTeachingClassIds).set(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


                //删除之前的数据

                teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                        .eq(TeachingClass::getTeachingClassId, teachingClassId).set(TeachingClass::getDeleted, Deleted.YES.getCode()));
                teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                        .eq(TeachingClassWeek::getTeachingClassId, teachingClassId).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));

                teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                        .eq(TeachingClassStudent::getTeachingClassId, teachingClassId).set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));
                teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                        .eq(TeachingClassTeacher::getTeachingClassId, teachingClassId).set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));

            }
            return "拆班班级所有班级已删除";
        }


        List<TeachingClassStudent> teachingClassStudentList = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>()
                .eq(TeachingClassStudent::getTeachingClassId, teachingClassId).eq(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));

        List<Long> studentIds = teachingClassStudentList.stream().map(TeachingClassStudent::getStudentId).distinct().collect(Collectors.toList());

        subTeachingClassId.remove(teachingClassId);

        List<TeachingClassStudent> teachingClassStudents = new ArrayList<>();

        int num = 0;
        for (Long studentId : studentIds) {
            TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
            teachingClassStudent.setTeachingClassId(subTeachingClassId.get(num % subTeachingClassId.size()));
            teachingClassStudent.setStudentId(studentId);
            teachingClassStudent.setFlag(1);
            teachingClassStudent.setDeleted(Deleted.NOT.getCode());
            num++;
            teachingClassStudents.add(teachingClassStudent);
        }

        if (CollUtil.isNotEmpty(teachingClassStudents)) {
            Consumer<List<TeachingClassStudent>> consumer = o -> teachingClassStudentMapper.addBatch(o);
            BatchInsert.batchSplitInsert(teachingClassStudents, consumer);
        }

        teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getTeachingClassId, teachingClassId).set(TeachingClass::getDeleted, Deleted.YES.getCode()));
        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClassId).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));

        teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                .eq(TeachingClassStudent::getTeachingClassId, teachingClassId).set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));
        teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                .eq(TeachingClassTeacher::getTeachingClassId, teachingClassId).set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));
        return uuid;
    }

    @Override
    public List<Student> getStudentList(Integer taskId, Long teachingClassId, String keyword) {
        TeachingClass teachingClass = teachingClassMapper.selectOne(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getTeachingClassId, teachingClassId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Student> students = studentMapper.selectList(new LambdaQueryWrapper<Student>().eq(Student::getTaskId, taskId)
                .eq(Student::getDeleted, Deleted.NOT.getCode())
                .and(StrUtil.isNotBlank(keyword), wrapper -> wrapper.like(Student::getStudentName, keyword)));

        List<TeachingClassStudent> teachingClassStudentList = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>()
                .eq(TeachingClassStudent::getTeachingClassId, teachingClass.getTeachingClassId())
                .eq(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));

        List<Student> result = students.stream().filter(p -> teachingClassStudentList.stream().anyMatch(o -> o.getStudentId().equals(p.getStudentId()))).collect(Collectors.toList());

        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addToRegularStudent(Integer taskId, Long fromTeachingClassId, Long toTeachingClassId, List<Long> studentDtos) {

//        List<TeachingClassStudent> teachingClassStudentList = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>()
//                .eq(TeachingClassStudent::getTeachingClassId, toTeachingClassId)
//                .in(TeachingClassStudent::getStudentId, studentDtos));

//        List<Long> enableStudentIds = teachingClassStudentList.stream().map(TeachingClassStudent::getStudentId).distinct().collect(Collectors.toList());


        teachingClassStudentService.lambdaUpdate().eq(TeachingClassStudent::getTeachingClassId, fromTeachingClassId)
                .in(TeachingClassStudent::getStudentId, studentDtos).remove();


//        studentDtos.removeAll(enableStudentIds);

        List<TeachingClassStudent> teachingClassStudents = new ArrayList<>();
        for (Long studentDto : studentDtos) {
            TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
            teachingClassStudent.setTeachingClassId(toTeachingClassId);
            teachingClassStudent.setStudentId(studentDto);
            teachingClassStudent.setFlag(1);
            teachingClassStudent.setDeleted(Deleted.NOT.getCode());
            teachingClassStudents.add(teachingClassStudent);
        }
//        if (CollUtil.isNotEmpty(enableStudentIds)) {
//            teachingClassStudentService.lambdaUpdate().eq(TeachingClassStudent::getTeachingClassId, toTeachingClassId)
//                    .eq(TeachingClassStudent::getTeachingClassId, toTeachingClassId).in(TeachingClassStudent::getStudentId, enableStudentIds)
//                    .set(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()).update();
//        }

        teachingClassStudentService.saveBatch(teachingClassStudents);


        return "学生已经转移";

    }

    @Override
    public TeachingClassByMerge getMixedClassInfoList(Integer taskId, Long teachingClassId, String gradeId, Long courseId, Integer isEnable) {


        TeachingClassByMerge teachingClassByMerge = new TeachingClassByMerge();

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(StringUtils.isNotBlank(gradeId), TeachingClass::getGradeId, gradeId)
                .eq(ObjUtil.isNotEmpty(courseId), TeachingClass::getCourseId, courseId)
                .eq(ObjUtil.isNotEmpty(isEnable), TeachingClass::getIsEnable, isEnable)
                .eq(ObjUtil.isNotEmpty(teachingClassId), TeachingClass::getTeachingClassId, teachingClassId)
                .isNotNull(TeachingClass::getUniqueShiftSign)
                .isNull(TeachingClass::getSubId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId)
                .distinct().collect(Collectors.toList());

        TaskInfo taskInfo = taskInfoService.getById(taskId);
        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>().eq(Room::getTerm, taskInfo.getTerm()).eq(Room::getSchoolId, taskInfo.getSchoolId()));

        if (CollUtil.isNotEmpty(teachingClassIds)) {

            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                    .in(TeachingClassWeek::getTeachingClassId, teachingClassIds).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

            List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                    .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


            List<TeachingClassMergeInfo> teachingClassMergeInfos = new ArrayList<>();


            teachingClassList.forEach(o -> {

                TeachingClassMergeInfo teachingClassMergeInfo = new TeachingClassMergeInfo();

                teachingClassMergeInfo.setTeachingClassId(o.getTeachingClassId());

                teachingClassMergeInfo.setTeachingClassName(o.getTeachingClassName());

                teachingClassMergeInfo.setGradeName(o.getGradeName());

                teachingClassMergeInfo.setGradeId(o.getGradeId());


                teachingClassMergeInfo.setCourseId(o.getCourseId());

                teachingClassMergeInfo.setCourseName(o.getCourseName());

                teachingClassMergeInfo.setTeachingClassType(o.getTeachingClassType());
                teachingClassMergeInfo.setClassNum(o.getClassNum());

                teachingClassMergeInfo.setEnable(o.getIsEnable());

                teachingClassMergeInfo.setState(o.getState());

                List<TeachingClassWeek> teachingClassWeeks = teachingClassWeekList.stream()
                        .filter(p -> p.getTeachingClassId().equals(o.getTeachingClassId())).collect(Collectors.toList());


                List<WeekDto> weekDtoList = new ArrayList<>();
                if (CollUtil.isNotEmpty(teachingClassWeeks)) {

                    for (TeachingClassWeek teachingClassWeek : teachingClassWeeks) {

                        WeekDto weekDto = new WeekDto();
                        weekDto.setAssessmentId(teachingClassWeek.getAssessmentId());
                        weekDto.setAssessmentName(teachingClassWeek.getAssessmentName());
                        weekDto.setHourType(teachingClassWeek.getHourType());
                        weekDto.setRoomId(teachingClassWeek.getRoomId());
                        if (ObjUtil.isNotNull(teachingClassWeek.getRoomId())) {
                            Room room = roomList.stream().filter(p -> p.getRoomId().equals(teachingClassWeek.getRoomId())).findFirst().orElse(null);
                            if (room != null) {
                                weekDto.setRoomName(room.getRoomName());
                            }
                        }
                        weekDto.setRoomType(teachingClassWeek.getRoomType());

                        weekDto.setRoomTypeName(GlobalConstant.roomTypeMap.get(teachingClassWeek.getRoomType()));
                        weekDto.setWeek(teachingClassWeek.getWeek());
                        weekDto.setConnectSection(teachingClassWeek.getConnectSection());
                        weekDto.setConnectNumber(teachingClassWeek.getConnectNumber());

                        Integer weekNum = getOneNumber(teachingClassWeek.getWeek());
                        weekDto.setTotalStudyHour(weekNum * teachingClassWeek.getWeekNum());

                        List<TeachingClassTeacher> teacher = teachingClassTeacherList.stream().filter(p -> p.getTeachingClassId()
                                .equals(o.getTeachingClassId()) && p.getTeacherType().equals(teachingClassWeek.getHourType())).collect(Collectors.toList());

                        if (CollUtil.isNotEmpty(teacher)) {
                            String collect = teacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).collect(Collectors.joining("、"));
                            String collect1 = teacher.stream().map(TeachingClassTeacher::getTeacherName).collect(Collectors.joining("、"));
                            weekDto.setTeacherId(collect);
                            weekDto.setTeacherName(collect1);
                        }


                        weekDto.setSign(teachingClassWeek.getSign());
                        weekDto.setStartWeek(teachingClassWeek.getStartWeek());
                        weekDto.setEndWeek(teachingClassWeek.getEndWeek());
                        weekDto.setWeekNum(teachingClassWeek.getWeekNum());
                        weekDtoList.add(weekDto);
                    }
                }


                teachingClassMergeInfo.setWeekDtoList(weekDtoList);

                teachingClassMergeInfos.add(teachingClassMergeInfo);
            });


            int sum = teachingClassMergeInfos.stream().mapToInt(TeachingClassMergeInfo::getState).sum();

            teachingClassMergeInfos.stream().sorted(Comparator.comparing(TeachingClassMergeInfo::getGradeName))
                    .collect(Collectors.toList());

            teachingClassByMerge.setEnable(sum);

            teachingClassByMerge.setUnable(teachingClassMergeInfos.size() - sum);

            teachingClassByMerge.setTotalClassNum(teachingClassMergeInfos.size());

            teachingClassByMerge.setTeachingClassMergeInfoList(teachingClassMergeInfos);


        }


        return teachingClassByMerge;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String MergeTeachingClassList(Integer taskId, MergeTeachingClassDto mergeTeachingClassDto) {


        //删除之前的合班数据
        if (StringUtils.isNotBlank(mergeTeachingClassDto.getSign())) {
            TeachingClass signTeachingClass = teachingClassMapper.selectOne(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                    .eq(TeachingClass::getUniqueShiftSign, mergeTeachingClassDto.getSign()).eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

            teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                    .eq(TeachingClass::getTeachingClassId, signTeachingClass.getTeachingClassId()).set(TeachingClass::getDeleted, Deleted.YES.getCode()));
            teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                    .eq(TeachingClassWeek::getTeachingClassId, signTeachingClass.getTeachingClassId()).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));
            teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                    .eq(TeachingClassTeacher::getTeachingClassId, signTeachingClass.getTeachingClassId()).set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));
            teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                    .eq(TeachingClassStudent::getTeachingClassId, signTeachingClass.getTeachingClassId()).set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));
        }


        //需要进行合班的班级
        List<TeachingClass> teachingClasses = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, mergeTeachingClassDto.getTeachingClassIds()));

        List<Long> deletedTeachingIds = teachingClasses.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

        TeachingClass mergeInfo = new TeachingClass();
        if (CollUtil.isNotEmpty(teachingClasses)) {
            mergeInfo = teachingClasses.stream().filter(p -> p.getTeachingClassId().equals(mergeTeachingClassDto.getToTeachingClassId())).findFirst().orElse(null);
        }

        List<TeachingClassStudent> teachingClassStudentList = teachingClassStudentMapper.selectList(new LambdaQueryWrapper<TeachingClassStudent>()
                .in(TeachingClassStudent::getTeachingClassId, mergeTeachingClassDto.getTeachingClassIds()));

        List<TeachingClassWeek> teachingClassWeek = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, mergeInfo.getTeachingClassId()));

        List<TeachingClassTeacher> teachingClassTeacher = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                .eq(TeachingClassTeacher::getTeachingClassId, mergeInfo.getTeachingClassId()));

        UUID uuid = UUID.randomUUID();
        TeachingClass teachingClass = new TeachingClass();
        if (mergeTeachingClassDto.getFlag().equals(1)) {
            String classId = "";
            String className = "";
            teachingClass.setTaskId(taskId);
            List<String> classIds = teachingClasses.stream().map(TeachingClass::getClassId).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(classIds)) {
                classId = String.join(",", classIds);
            }
            List<String> classNames = teachingClasses.stream().map(TeachingClass::getClassName).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(classNames)) {
                className = String.join(",", className);
            }
            teachingClass.setClassId(classId);
            teachingClass.setClassName(className);
            teachingClass.setTeachingClassName(mergeInfo.getTeachingClassName());
            teachingClass.setCourseId(mergeInfo.getCourseId());
            teachingClass.setCourseName(mergeInfo.getCourseName());
            int sum = teachingClasses.stream().mapToInt(TeachingClass::getClassNum).sum();
            teachingClass.setClassNum(sum);
            teachingClass.setGradeId(mergeInfo.getGradeId());
            teachingClass.setGradeName(mergeInfo.getGradeName());
            teachingClass.setTeachingClassType(TeachingClassType.NOSELECT.getCode());
            teachingClass.setFlag(mergeInfo.getFlag());
            teachingClass.setCampusId(mergeInfo.getCampusId());
            teachingClass.setCampusName(mergeInfo.getCampusName());
            teachingClass.setUniqueShiftSign(uuid.toString());
            teachingClass.setState(mergeInfo.getState());
            String collect = mergeTeachingClassDto.getTeachingClassIds().stream().map(String::valueOf).collect(Collectors.joining(","));
            teachingClass.setReserve(collect);
            teachingClassService.save(teachingClass);
        } else {
            teachingClass.setTaskId(taskId);
            teachingClass.setTeachingClassName(mergeInfo.getTeachingClassName());
            teachingClass.setCourseId(mergeInfo.getCourseId());
            teachingClass.setCourseName(mergeInfo.getCourseName());
            int sum = teachingClasses.stream().mapToInt(TeachingClass::getClassNum).sum();
            teachingClass.setClassNum(sum);
            teachingClass.setGradeId(mergeInfo.getGradeId());
            teachingClass.setGradeName(mergeInfo.getGradeName());
            teachingClass.setTeachingClassType(TeachingClassType.SELECT.getCode());
            teachingClass.setFlag(mergeInfo.getFlag());
            teachingClass.setCampusId(mergeInfo.getCampusId());
            teachingClass.setCampusName(mergeInfo.getCampusName());
            teachingClass.setUniqueShiftSign(uuid.toString());
            teachingClass.setIsEnable(6);
            teachingClass.setState(mergeInfo.getState());
            String collect = mergeTeachingClassDto.getTeachingClassIds().stream().map(String::valueOf).collect(Collectors.joining(","));
            teachingClass.setReserve(collect);
            teachingClassService.save(teachingClass);
        }


        // 添加teachingClassWeek

        List<TeachingClassWeek> teachingClassWeeks = new ArrayList<>();
        for (TeachingClassWeek tw : teachingClassWeek) {
            TeachingClassWeek tClassWeek = new TeachingClassWeek();
            tClassWeek.setTeachingClassId(teachingClass.getTeachingClassId());
            tClassWeek.setWeek(tw.getWeek());
            tClassWeek.setWeekNum(tw.getWeekNum());
            tClassWeek.setRoomId(null);
            tClassWeek.setSign(tw.getSign());
            tClassWeek.setStartWeek(tw.getStartWeek());
            tClassWeek.setEndWeek(tw.getEndWeek());
            tClassWeek.setRoomType(tw.getRoomType());
            tClassWeek.setConnectSection(tw.getConnectSection());
            tClassWeek.setConnectNumber(tw.getConnectNumber());
            tClassWeek.setAssessmentId(tw.getAssessmentId());
            tClassWeek.setAssessmentName(tw.getAssessmentName());
            tClassWeek.setHourType(tw.getHourType());
            teachingClassWeeks.add(tClassWeek);
        }

//        teachingClassWeekService.saveBatch(teachingClassWeeks);

        Consumer<List<TeachingClassWeek>> studentConsumer = o -> teachingClassWeekMapper.addBatch(o);
        BatchInsert.batchSplitInsert(teachingClassWeeks, studentConsumer);

        List<TeachingClassStudent> teachingClassStudents = new ArrayList<>();

        for (TeachingClassStudent student : teachingClassStudentList) {

            TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
            teachingClassStudent.setTeachingClassId(teachingClass.getTeachingClassId());
            teachingClassStudent.setStudentId(student.getStudentId());
            teachingClassStudent.setFlag(1);
            teachingClassStudent.setDeleted(Deleted.NOT.getCode());
            teachingClassStudents.add(teachingClassStudent);
        }

        if (CollUtil.isNotEmpty(teachingClassStudents)) {
//            teachingClassStudentService.saveBatch(teachingClassStudents);
            Consumer<List<TeachingClassStudent>> consumer = o -> teachingClassStudentMapper.addBatch(o);
            BatchInsert.batchSplitInsert(teachingClassStudents, consumer);
        }


        List<TeachingClassTeacher> teacher = new ArrayList<>();
        if (CollUtil.isNotEmpty(teachingClassTeacher)) {
            teachingClassTeacher.forEach(o -> {
                TeachingClassTeacher teacher1 = new TeachingClassTeacher();
                teacher1.setTeachingClassId(teachingClass.getTeachingClassId());
                teacher1.setTeacherId(o.getTeacherId());
                teacher1.setTeacherName(o.getTeacherName());
                teacher1.setTeacherType(o.getTeacherType());
                teacher.add(teacher1);

            });
            teachingClassTeacherService.saveBatch(teacher);
        }

        //删除之前数据
        teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .in(TeachingClass::getTeachingClassId, deletedTeachingIds)
                .set(TeachingClass::getDeleted, Deleted.YES.getCode()));


        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>().
                in(TeachingClassWeek::getTeachingClassId, deletedTeachingIds).set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));

        teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, deletedTeachingIds)
                .set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));

        teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                .in(TeachingClassStudent::getTeachingClassId, deletedTeachingIds)
                .set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));


        return uuid.toString();

    }

    @Override
    public TeachingClassMergeInfo getMergeTeachingClass(Integer taskId, String uuid) {
        TeachingClass teachingClass = teachingClassMapper.selectOne(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getUniqueShiftSign, uuid)
                .isNull(TeachingClass::getSubId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));
        TeachingClassMergeInfo teachingClassMergeInfo = new TeachingClassMergeInfo();
        teachingClassMergeInfo.setTeachingClassId(teachingClass.getTeachingClassId());
        teachingClassMergeInfo.setTeachingClassName(teachingClass.getTeachingClassName());
        teachingClassMergeInfo.setCourseId(teachingClass.getCourseId());
        teachingClassMergeInfo.setCourseName(teachingClass.getCourseName());
        teachingClassMergeInfo.setClassNum(teachingClass.getClassNum());
        TeachingClassTeacher teachingClassTeacher = teachingClassTeacherMapper.selectOne(new LambdaQueryWrapper<TeachingClassTeacher>()
                .eq(TeachingClassTeacher::getTeachingClassId, teachingClass.getTeachingClassId())
                .eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));
        teachingClassMergeInfo.setTeacherId(teachingClassTeacher.getTeacherId());
        teachingClassMergeInfo.setTeacherName(teachingClassTeacher.getTeacherName());
        teachingClassMergeInfo.setEnable(teachingClass.getIsEnable());
        teachingClassMergeInfo.setState(teachingClass.getState());

        List<WeekDto> weekDtos = new ArrayList<>();
        List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClass.getTeachingClassId()).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

        Course course = courseMapper.selectOne(new LambdaQueryWrapper<Course>().eq(Course::getTaskId, taskId).eq(Course::getCourseId, teachingClass.getCourseId())
                .eq(Course::getDeleted, Deleted.NOT.getCode()));

        teachingClassWeekList.forEach(o -> {
            WeekDto weekDto = new WeekDto();
            weekDto.setAssessmentId(o.getAssessmentId());
            weekDto.setAssessmentName(o.getAssessmentName());
            weekDto.setHourType(o.getHourType());
            weekDto.setWeek(o.getWeek());
            if (o.getHourType() == 1) {
                weekDto.setTotalStudyHour(course.getLectureHour());
            } else if (o.getHourType() == 2) {
                weekDto.setTotalStudyHour(course.getExperimentalHour());
            } else if (o.getHourType() == 3) {
                weekDto.setTotalStudyHour(course.getComputerHour());
            } else if (o.getHourType() == 4) {
                weekDto.setTotalStudyHour(course.getOtherHour());
            }
            weekDto.setWeekNum(o.getWeekNum());
            weekDto.setConnectNumber(o.getConnectNumber());
            weekDto.setConnectSection(o.getConnectSection());
            weekDtos.add(weekDto);
        });
        teachingClassMergeInfo.setWeekDtoList(weekDtos);
        return teachingClassMergeInfo;
    }


    @Override
    public String updateBatchAssessment(Integer taskId, List<Long> teachingClassIds, Integer assessmentId) {
        AssessmentMethod assessmentMethod = assessmentMethodService.lambdaQuery().eq(AssessmentMethod::getAssessmentId, assessmentId).one();
        if (assessmentMethod != null) {
            teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, teachingClassIds)
                    .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                    .set(TeachingClassWeek::getAssessmentId, assessmentMethod.getAssessmentId())
                    .set(TeachingClassWeek::getAssessmentName, assessmentMethod.getAssessmentName()));
        }
        return "考核方式已批量修改";

    }

    @Override
    public String updateBatchConnectSection(Integer taskId, List<Long> teachingClassIds, Integer connectSection) {
        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                .set(TeachingClassWeek::getConnectSection, connectSection));
        return "连上节次已批量修改";
    }

    @Override
    public String updateBatchConnectNumber(Integer taskId, List<Long> teachingClassIds, Integer connectNumber) {
        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                .set(TeachingClassWeek::getConnectNumber, connectNumber));
        return "连上次数已批量修改";
    }

    @Override
    public Result updateBatchWeek(Integer taskId, UpdateBatchWeekInput teachingClassIds) {

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .in(TeachingClass::getTeachingClassId, teachingClassIds.getTeachingClassIds())
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));


        //过滤单双拆班教学班
        List<TeachingClass> collect = teachingClassList.stream().filter(p -> StringUtils.isNotBlank(p.getUniqueShiftSign()) && ObjUtil.isNotNull(p.getSubId()) && p.getSubId().equals(0)).distinct().collect(Collectors.toList());

        if (CollUtil.isNotEmpty(collect)) {

            teachingClassList.removeAll(collect);

        }

        if (CollUtil.isNotEmpty(teachingClassList)) {

            List<Long> segmentTeachingClass = teachingClassList.stream().filter(p -> p.getFlag().equals(2)).distinct()
                    .map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

            if (CollUtil.isNotEmpty(segmentTeachingClass)) {

                teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                        .in(TeachingClassWeek::getTeachingClassId, segmentTeachingClass)
                        .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                        .set(TeachingClassWeek::getSign, teachingClassIds.getSign())
                        .set(TeachingClassWeek::getStartWeek, teachingClassIds.getStartWeek())
                        .set(TeachingClassWeek::getEndWeek, teachingClassIds.getEndWeek())
                        .set(TeachingClassWeek::getWeek, teachingClassIds.getWeek()));

            }

            Integer wn = getOneNumber(teachingClassIds.getWeek());

            if (teachingClassIds.getTotalHour() % wn != 0) {
                return Result.ofSuccess("总学时和周次之间不是倍数关系");
            }


            Integer weekNum = teachingClassIds.getTotalHour() / wn;

            if (ObjUtil.isNotNull(teachingClassIds.getConnectSection()) && teachingClassIds.getConnectSection()==0 || ObjUtil.isNotNull(teachingClassIds.getConnectNumber()) && teachingClassIds.getConnectNumber()==0) {
                return Result.ofFail("连上节次和连上次数不能为0");
            }

            if (weekNum < teachingClassIds.getConnectSection() || weekNum < (teachingClassIds.getConnectNumber() * teachingClassIds.getConnectSection())) {

                return Result.ofFail("周学时小于连上节次和连上次数");
            }


            List<Long> courseTeachingClass = teachingClassList.stream().filter(p -> p.getFlag().equals(1)).distinct()
                    .map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());


            if (CollUtil.isNotEmpty(courseTeachingClass)) {
                teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                        .in(TeachingClassWeek::getTeachingClassId, courseTeachingClass)
                        .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                        .eq(ObjUtil.isNotNull(teachingClassIds.getHourType()), TeachingClassWeek::getHourType, teachingClassIds.getHourType())
                        .set(TeachingClassWeek::getWeek, teachingClassIds.getWeek())
                        .set(TeachingClassWeek::getSign, teachingClassIds.getSign())
                        .set(TeachingClassWeek::getStartWeek, teachingClassIds.getStartWeek())
                        .set(TeachingClassWeek::getEndWeek, teachingClassIds.getEndWeek())
                        .set(TeachingClassWeek::getConnectNumber, teachingClassIds.getConnectNumber())
                        .set(TeachingClassWeek::getConnectSection, teachingClassIds.getConnectSection())
                        .set(TeachingClassWeek::getWeekNum, weekNum));
            }

            return Result.ofSuccess("教学班信息设置成功");
        }

        return Result.ofFail("教学班数量不能为空");


//        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
//                .eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, teachingClassIds.getTeachingClassIds())
//                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

//        List<Long> courseTeachingClass = teachingClassList.stream().filter(p -> p.getFlag().equals(1)).distinct().collect(Collectors.toList()).stream()
//                .map(TeachingClass::getTeachingClassId).collect(Collectors.toList());
//
//        List<Long> segmentTeachingClass = teachingClassList.stream().filter(p -> p.getFlag().equals(2)).distinct().collect(Collectors.toList()).stream()
//                .map(TeachingClass::getTeachingClassId).collect(Collectors.toList());


    }

    @Override
    public List<OtherTeachingClassList> getOtherTeachingClassList(Integer taskId, Long teachingClassId, String uuid) {
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getUniqueShiftSign, uuid).eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClass> otherList = teachingClassList.stream().filter(p -> !p.getTeachingClassId().equals(teachingClassId)).collect(Collectors.toList());
        List<OtherTeachingClassList> otherTeachingClassLists = new ArrayList<>();
        otherList.forEach(o -> {
            OtherTeachingClassList otherTeachingClassList = new OtherTeachingClassList();
            otherTeachingClassList.setTeachingClassId(o.getTeachingClassId());
            otherTeachingClassList.setTeachingClassName(o.getTeachingClassName());
            otherTeachingClassLists.add(otherTeachingClassList);
        });

        return otherTeachingClassLists;
    }

    @Override
    public Result updateWeek(Integer taskId, Long teachingClassId, Integer hourType, String week, Integer
            totalHour, Integer flag, Integer connectSection, Integer connectNumber, Integer sign, Integer startWeek, Integer endWeek) {

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId).eq(TeachingClass::getTeachingClassId, teachingClassId).eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        if (CollUtil.isNotEmpty(teachingClassList)) {
            TeachingClass teachingClass = teachingClassList.get(0);

            if (StringUtils.isNotBlank(teachingClass.getUniqueShiftSign()) && ObjUtil.isNotNull(teachingClass.getSubId()) && teachingClass.getSubId().equals(0)) {

                return Result.ofFail("单双班拆班不允许更改周学时");
            }

        }


        if (flag == 2) {
            teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                    .eq(TeachingClassWeek::getTeachingClassId, teachingClassId)
                    .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                    .set(TeachingClassWeek::getWeek, week).set(TeachingClassWeek::getStartWeek, startWeek)
                    .set(TeachingClassWeek::getEndWeek, endWeek).set(TeachingClassWeek::getSign, sign));
            return Result.ofSuccess("环节周次设置完毕");
        }

        Integer wn = getOneNumber(week);

        if (totalHour % wn != 0) {
            return Result.ofFail("总学时和周次之间不是倍数关系");
        }

        Integer weekNum = totalHour / wn;


        if ((ObjUtil.isNotNull(connectSection) && connectSection==0) || (ObjUtil.isNotNull(connectNumber) && connectNumber==0)) {
            return Result.ofFail("连上节次和连上次数不能为0");
        }

        if (weekNum < connectSection || (weekNum < (connectNumber * connectSection))) {

            return Result.ofFail("周学时小于连上节次和连上次数之和");
        }

        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClassId)
                .eq(TeachingClassWeek::getHourType, hourType)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                .set(TeachingClassWeek::getWeek, week)
                .set(TeachingClassWeek::getSign, sign)
                .set(TeachingClassWeek::getStartWeek, startWeek)
                .set(TeachingClassWeek::getEndWeek, endWeek)
                .set(TeachingClassWeek::getConnectNumber, connectNumber)
                .set(TeachingClassWeek::getConnectSection, connectSection)
                .set(TeachingClassWeek::getWeekNum, weekNum));


        return Result.ofSuccess("教学班课程信息设置成功");


    }

    @Override
    public String updateConnectNumber(Integer taskId, Long teachingClassId, Integer hourType, Integer connectNumber) {
        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClassId)
                .eq(TeachingClassWeek::getHourType, hourType)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                .set(TeachingClassWeek::getConnectNumber, connectNumber));
        return "连上次数已修改";
    }

    @Override
    public String updateConnectSection(Integer taskId, Long teachingClassId, Integer hourType, Integer
            connectSection) {
        teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClassId)
                .eq(TeachingClassWeek::getHourType, hourType)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                .set(TeachingClassWeek::getConnectSection, connectSection));
        return "连上节次已修改";
    }



    @Override
    public Boolean whetherMixClass(Integer taskId, List<Long> teachingClassIds) {
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, teachingClassIds)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));


        List<String> gradeIds = teachingClassList.stream().map(TeachingClass::getGradeId).distinct().collect(Collectors.toList());

        if (CollUtil.isNotEmpty(gradeIds)) {
            if (gradeIds.size() != 1) {
                return false;
            }
        }

        List<Long> courseIds = teachingClassList.stream().map(TeachingClass::getCourseId).distinct().collect(Collectors.toList());
        if (CollUtil.isNotEmpty(courseIds)) {
            if (courseIds.size() != 1) {
                return false;
            }
        }

        TeachingClass teachingClass = teachingClassList.get(0);
        if (teachingClass.getFlag().equals(2)) {
            return false;
        }

        return true;

    }

    @Override
    public List<CourseInformation> getTeachingClassList(Integer taskId, List<Long> teachingClassIds) {
        List<CourseInformation> retList = new ArrayList<>();

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, teachingClassIds)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, teachingClassIds).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

        teachingClassList.forEach(o -> {
            CourseInformation courseInformation = new CourseInformation();
            courseInformation.setTeachingClassId(o.getTeachingClassId());
            courseInformation.setTeachingClassName(o.getTeachingClassName());
            courseInformation.setCourseId(o.getCourseId());
            courseInformation.setCourseName(o.getCourseName());
            courseInformation.setFlag(o.getFlag());
            courseInformation.setIsEnable(o.getIsEnable());
            courseInformation.setClassNum(o.getClassNum());
            List<TeachingClassWeek> teachingClassWeeks = teachingClassWeekList.stream().filter(p -> p.getTeachingClassId()
                    .equals(o.getTeachingClassId())).collect(Collectors.toList());

            List<WeekDto> weekDtos = new ArrayList<>();
            teachingClassWeeks.forEach(tw -> {

                WeekDto weekDto = new WeekDto();
                weekDto.setAssessmentId(tw.getAssessmentId());
                weekDto.setAssessmentName(tw.getAssessmentName());
                weekDto.setWeek(tw.getWeek());
                weekDto.setWeekNum(tw.getWeekNum());
                weekDto.setHourType(tw.getHourType());
                weekDto.setConnectNumber(tw.getConnectNumber());
                weekDto.setConnectSection(tw.getConnectSection());
                weekDto.setRoomId(tw.getRoomId());
                weekDto.setRoomType(tw.getRoomType());

                List<TeachingClassTeacher> teacher = teachingClassTeacherList.stream().filter(p -> p.getTeachingClassId()
                        .equals(o.getTeachingClassId()) && p.getTeacherType().equals(tw.getHourType())).collect(Collectors.toList());

                if (CollUtil.isNotEmpty(teacher)) {
                    String collect = teacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).collect(Collectors.joining("、"));
                    String collect1 = teacher.stream().map(TeachingClassTeacher::getTeacherName).collect(Collectors.joining("、"));
                    weekDto.setTeacherId(collect);
                    weekDto.setTeacherName(collect1);
                }
                weekDto.setRoomTypeName(GlobalConstant.roomTypeMap.get(tw.getRoomType()));
                Integer weekNum = getOneNumber(tw.getWeek());
                weekDto.setTotalStudyHour(weekNum * tw.getWeekNum());
                weekDtos.add(weekDto);
            });
            courseInformation.setWeekDtoList(weekDtos);

            retList.add(courseInformation);
        });

        return retList;

    }


    @Override
    public TeachingClassMergeInfo getMergeTeaching(Integer taskId, String uuid) {
        TeachingClass teachingClass = teachingClassMapper.selectOne(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getUniqueShiftSign, uuid).eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>()
                .eq(TeachingClassWeek::getTeachingClassId, teachingClass.getTeachingClassId()).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

        List<TeachingClassTeacher> teachingClassTeacher = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                .eq(TeachingClassTeacher::getTeachingClassId, teachingClass.getTeachingClassId()).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

        TeachingClassMergeInfo teachingClassMergeInfo = new TeachingClassMergeInfo();

        teachingClassMergeInfo.setTeachingClassId(teachingClass.getTeachingClassId());
        teachingClassMergeInfo.setTeachingClassName(teachingClass.getTeachingClassName());
        teachingClassMergeInfo.setClassNum(teachingClass.getClassNum());
        teachingClassMergeInfo.setCourseName(teachingClass.getCourseName());
        teachingClassMergeInfo.setCourseId(teachingClass.getCourseId());
        teachingClassMergeInfo.setGradeId(teachingClass.getGradeId());
        teachingClassMergeInfo.setGradeName(teachingClass.getGradeName());


        List<WeekDto> weekDtoList = new ArrayList<>();

        teachingClassWeekList.forEach(o -> {
            WeekDto weekDto = new WeekDto();
            weekDto.setAssessmentId(o.getAssessmentId());
            weekDto.setAssessmentName(o.getAssessmentName());
            weekDto.setWeek(o.getWeek());
            weekDto.setWeekNum(o.getWeekNum());
            weekDto.setHourType(o.getHourType());
            weekDto.setConnectNumber(o.getConnectNumber());
            weekDto.setConnectSection(o.getConnectSection());
            weekDto.setStartWeek(o.getStartWeek());
            weekDto.setEndWeek(o.getEndWeek());
            weekDto.setSign(o.getSign());
            weekDto.setRoomId(o.getRoomId());
            weekDto.setRoomType(o.getRoomType());

            List<TeachingClassTeacher> teacher = teachingClassTeacher.stream().filter(p -> p.getTeachingClassId()
                    .equals(o.getTeachingClassId()) && p.getTeacherType().equals(o.getHourType())).collect(Collectors.toList());

            if (CollUtil.isNotEmpty(teacher)) {
                String collect = teacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).collect(Collectors.joining("、"));
                String collect1 = teacher.stream().map(TeachingClassTeacher::getTeacherName).collect(Collectors.joining("、"));
                weekDto.setTeacherId(collect);
                weekDto.setTeacherName(collect1);
            }
            weekDto.setRoomTypeName(GlobalConstant.roomTypeMap.get(o.getRoomType()));
            Integer weekNum = getOneNumber(o.getWeek());
            weekDto.setTotalStudyHour(weekNum * o.getWeekNum());
            weekDtoList.add(weekDto);
        });
        teachingClassMergeInfo.setWeekDtoList(weekDtoList);

        return teachingClassMergeInfo;
    }

    @Override
    public void classToExcel(HttpServletResponse response, Integer taskId, Integer typeId, String typeName, Long
            classId, String className, String gradeId, String gradeName, Long courseId, String courseName, Integer isEnable) {
        TaskInfo taskInfo = taskInfoService.getById(taskId);

        List<ClassExcelVo> classExcelVoArrayList = new ArrayList<>();


        //构建标题
        String title = " {} {}行政班信息导出 " + "\n" + "" +
                "年级:{}   课程:{}" + "\n";
        String format = StrUtil.format(title, taskInfo.getTermName(), taskInfo.getSchoolName()
                , StringUtils.isNotBlank(gradeName) ? gradeName : "全部年级",
                StringUtils.isNotBlank(courseName) ? courseName : "全部课程");

        TeachingClassByClass classInfoList = this.getClassInfoListV2(taskId, gradeId, typeId, courseId, classId, isEnable, -1, 1);

        List<ClassInfor> classInforList = classInfoList.getClassInforList();


        for (ClassInfor o : classInforList) {

            ClassExcelVo classExcelVo = new ClassExcelVo();

            classExcelVo.setClassName(o.getClassName());
            classExcelVo.setClassNum(o.getClassNumber());

            List<TeachingClassExcelVo> teachingClassExcelVos = new ArrayList<>();

            List<CourseInformation> courseInformationList = o.getCourseInformationList();


            for (CourseInformation c : courseInformationList) {

                TeachingClassExcelVo teachingClassExcelVo = new TeachingClassExcelVo();

                teachingClassExcelVo.setCourseName(c.getCourseName());

                teachingClassExcelVo.setTeachingClassName(c.getTeachingClassName());

                List<WeekDto> weekDtoList = c.getWeekDtoList();

                List<TeachingClassWeekExcelVo> teachingClassWeekExcelVos = new ArrayList<>();

                for (WeekDto w : weekDtoList) {

                    TeachingClassWeekExcelVo teachingClassWeekExcelVo = new TeachingClassWeekExcelVo();

                    if (ObjUtil.isNull(w.getHourType())) {

                        teachingClassWeekExcelVo.setHourType("--");
                        teachingClassWeekExcelVo.setTotalHour("--");
                        teachingClassWeekExcelVo.setWeekNum("--");
                    }

                    if (w.getHourType() != null) {

                        if (w.getHourType().equals(1)) {
                            teachingClassWeekExcelVo.setHourType("讲授");
                        } else if (w.getHourType().equals(2)) {
                            teachingClassWeekExcelVo.setHourType("上机");
                        } else if (w.getHourType().equals(3)) {
                            teachingClassWeekExcelVo.setHourType("实验");
                        } else if (w.getHourType().equals(4)) {
                            teachingClassWeekExcelVo.setHourType("其他");
                        }
                    }
                    teachingClassExcelVo.setAssessmentName(StringUtils.isNotBlank(w.getAssessmentName()) ? w.getAssessmentName() : "--");
                    teachingClassWeekExcelVo.setTeacherName(w.getTeacherName());
                    teachingClassWeekExcelVo.setConnectNumber(ObjUtil.isNotNull(w.getConnectNumber()) ? String.valueOf(w.getConnectNumber()) : "--");
                    teachingClassWeekExcelVo.setConnectSection(ObjUtil.isNotNull(w.getConnectSection()) ? String.valueOf(w.getConnectSection()) : "--");
                    teachingClassWeekExcelVo.setRoomType(ObjUtil.isNotNull(w.getRoomType()) ? GlobalConstant.roomTypeMap.get(w.getRoomType()) : "--");
                    Integer oneNumber = getOneNumber(w.getWeek());
                    if (w.getWeekNum() != null) {
                        teachingClassWeekExcelVo.setTotalHour(String.valueOf(oneNumber * w.getWeekNum()));
                        teachingClassWeekExcelVo.setWeekNum(String.valueOf(w.getWeekNum()));
                    }

                    teachingClassWeekExcelVo.setWeek((w.getWeek().indexOf("1") + 1) + "-" + (w.getWeek().lastIndexOf("1") + 1));
                    teachingClassWeekExcelVos.add(teachingClassWeekExcelVo);
                }

                teachingClassExcelVo.setTeachingClassWeekExcelVoList(teachingClassWeekExcelVos);

                teachingClassExcelVo.setIsEnable((c.getIsEnable() == 0 || c.getIsEnable() == 6) ? "已启用" : "未启用");

                teachingClassExcelVos.add(teachingClassExcelVo);


                classExcelVo.setTeachingClassExcelVoList(teachingClassExcelVos);
            }
            classExcelVoArrayList.add(classExcelVo);
        }

        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));

        List<ClassExcelVo> collect = classExcelVoArrayList.stream().distinct().collect(Collectors.toList());
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");

        MyExcelUtils.exportExcel(collect,
                format,
                "行政班信息",
                ClassExcelVo.class,
                "行政班信息导出" + time + ".xls", response);

    }

    @Override
    public void selectClassToExcel(HttpServletResponse response, Integer taskId, Long teachingClassId, String
            teachingClassName, String gradeId, String gradeName, Long courseId, String courseName, Integer isEnable) {
        List<SelectClassExcelVo> selectClassExcelVoArrayList = new ArrayList<>();

        TaskInfo taskInfo = taskInfoService.getById(taskId);


        //构建标题
        String title = "{} {}选课教学班信息导出" + "\n" + "" +
                "年级:{}  课程:{}" + "\n";
        String format = StrUtil.format(title, taskInfo.getTermName(), taskInfo.getSchoolName()
                , StringUtils.isNotBlank(gradeName) ? gradeName : "全部年级",
                StringUtils.isNotBlank(courseName) ? courseName : "全部课程");


        TeachingClassBySelect selectClassList = this.getSelectClassList(taskId, teachingClassId, gradeId, courseId, isEnable, -1, 1);
        List<TeachingClassInfo> teachingClassInfoList = selectClassList.getTeachingClassInfoList();

        if (CollUtil.isNotEmpty(teachingClassInfoList)) {
            teachingClassInfoList.forEach(o -> {
                SelectClassExcelVo selectClassExcelVo = new SelectClassExcelVo();
                selectClassExcelVo.setTeachingClassName(o.getTeachingClassName());
                selectClassExcelVo.setCourseName(o.getCourseName());
                selectClassExcelVo.setClassNum(o.getClassNum());
                selectClassExcelVo.setIsEnable(o.getEnable() == 0 ? "已启用" : "未启用");
                List<SelectTeachingClassExcelVo> selectTeachingClassExcelVos = new ArrayList<>();
                List<WeekDto> weekDtoList = o.getWeekDtoList();
                weekDtoList.forEach(w -> {
                    SelectTeachingClassExcelVo selectTeachingClassExcelVo = new SelectTeachingClassExcelVo();
                    selectClassExcelVo.setAssessmentName(w.getAssessmentName());
                    if (w.getHourType().equals(1)) {
                        selectTeachingClassExcelVo.setHourType("讲授");
                    } else if (w.getHourType().equals(2)) {
                        selectTeachingClassExcelVo.setHourType("实验");
                    } else if (w.getHourType().equals(3)) {
                        selectTeachingClassExcelVo.setHourType("上机");

                    } else if (w.getHourType().equals(4)) {
                        selectTeachingClassExcelVo.setHourType("其他");
                    }

                    selectTeachingClassExcelVo.setTeacherName(w.getTeacherName());
                    selectTeachingClassExcelVo.setConnectNumber(w.getConnectNumber());
                    selectTeachingClassExcelVo.setConnectSection(w.getConnectSection());
                    selectTeachingClassExcelVo.setRoomType(ObjUtil.isNotNull(w.getRoomType()) ? GlobalConstant.roomTypeMap.get(w.getRoomType()) : "暂未设置场地类型");
                    Integer oneNumber = getOneNumber(w.getWeek());
                    selectTeachingClassExcelVo.setTotalHour(oneNumber * w.getWeekNum());
                    selectTeachingClassExcelVo.setWeekNum(w.getWeekNum());
                    selectTeachingClassExcelVo.setWeek((w.getWeek().indexOf("1") + 1) + "-" + (w.getWeek().lastIndexOf("1") + 1));
                    selectTeachingClassExcelVos.add(selectTeachingClassExcelVo);

                });
                selectClassExcelVo.setIsEnable((o.getEnable() == 0 || o.getEnable() == 6) ? "已启用" : "未启用");
                selectClassExcelVo.setSelectTeachingClassExcelVoList(selectTeachingClassExcelVos);
                selectClassExcelVoArrayList.add(selectClassExcelVo);
            });
        }

        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));


        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");

        MyExcelUtils.exportExcel(selectClassExcelVoArrayList,
                format,
                "选课教学班信息",
                SelectClassExcelVo.class,
                "选课教学班信息导出" + time + ".xls", response);


    }

    @Override
    public void subClassToExcel(HttpServletResponse response, Integer taskId, Long teachingClassId, String
            teachingClassName, Integer subType, String subTypeName, String gradeId, String gradeName, Long courseId, String
                                        courseName, Integer isEnable) {

        List<SubClassToExcel> subClassToExcelList = new ArrayList<>();
        TaskInfo taskInfo = taskInfoService.getById(taskId);
        String title = "{} {}拆班教学班信息导出" + "\n" + "" +
                "  年级:{}  课程:{}" + "\n";
        String format = StrUtil.format(title, taskInfo.getTermName(), taskInfo.getSchoolName()
                , StringUtils.isNotBlank(gradeName) ? gradeName : "全部年级",
                StringUtils.isNotBlank(courseName) ? courseName : "全部课程");


        TeachingClassBySub subClassInfoList = this.getSubClassInfoList(taskId, teachingClassId, null, gradeId, courseId, isEnable, -1, 1);

        List<TeachingClassSubInfo> teachingClassSubInfoList = subClassInfoList.getTeachingClassSubInfoList();

        if (CollUtil.isNotEmpty(teachingClassSubInfoList)) {

            teachingClassSubInfoList.forEach(o -> {
                SubClassToExcel subClassToExcel = new SubClassToExcel();
                subClassToExcel.setTeachingClassName(o.getTeachingClassName());
                subClassToExcel.setCourseName(o.getCourseName());
                subClassToExcel.setSubType(o.getSubName());
                subClassToExcel.setClassNum(o.getClassNum());
                subClassToExcel.setIsEnable(o.getEnable() == 0 ? "已启用" : "未启用");

                List<WeekDto> weekDtoList = o.getWeekDtoList();

                List<SubTeachingClassExcelVo> subTeachingClassExcelVoList = new ArrayList<>();

                weekDtoList.forEach(w -> {
                    SubTeachingClassExcelVo subTeachingClassExcelVo = new SubTeachingClassExcelVo();
                    subClassToExcel.setAssessmentName(w.getAssessmentName());
                    if (w.getHourType().equals(1)) {
                        subTeachingClassExcelVo.setHourType("讲授");
                    } else if (w.getHourType().equals(2)) {
                        subTeachingClassExcelVo.setHourType("实验");
                    } else if (w.getHourType().equals(3)) {
                        subTeachingClassExcelVo.setHourType("上机");

                    } else if (w.getHourType().equals(4)) {
                        subTeachingClassExcelVo.setHourType("其他");
                    }

                    subTeachingClassExcelVo.setTeacherName(w.getTeacherName());
                    subTeachingClassExcelVo.setConnectNumber(w.getConnectNumber());
                    subTeachingClassExcelVo.setConnectSection(w.getConnectSection());
                    subTeachingClassExcelVo.setRoomType(ObjUtil.isNotNull(w.getRoomType()) ? GlobalConstant.roomTypeMap.get(w.getRoomType()) : "暂未设置场地类型");
                    Integer oneNumber = getOneNumber(w.getWeek());
                    subTeachingClassExcelVo.setTotalHour(oneNumber * w.getWeekNum());
                    subTeachingClassExcelVo.setWeekNum(w.getWeekNum());
                    subTeachingClassExcelVo.setWeek((w.getWeek().indexOf("1") + 1) + "-" + (w.getWeek().lastIndexOf("1") + 1));
                    subTeachingClassExcelVoList.add(subTeachingClassExcelVo);

                });

                subClassToExcel.setSelectTeachingClassExcelVoList(subTeachingClassExcelVoList);
                subClassToExcelList.add(subClassToExcel);

            });
        }


        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));


        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");

        MyExcelUtils.exportExcel(subClassToExcelList,
                format,
                "小组信息",
                SubClassToExcel.class,
                "拆班教学班信息导出" + time + ".xls", response);

    }

    @Override
    public void mixClassToExcel(HttpServletResponse response, Integer taskId, Long teachingClassId, String
            teachingClassName, String gradeId, String gradeName, Long courseId, String courseName, Integer isEnable) {
        List<MixClassToExcel> mixClassToExcelList = new ArrayList<>();

        TaskInfo taskInfo = taskInfoService.getById(taskId);

        String title = "{} {}合班教学班信息导出" + "\n" +
                "年级:{}  课程:{}" + "\n";
        String format = StrUtil.format(title, taskInfo.getTermName(), taskInfo.getSchoolName()
                , StringUtils.isNotBlank(gradeName) ? gradeName : "全部年级",
                StringUtils.isNotBlank(courseName) ? courseName : "全部课程");

        TeachingClassByMerge mixedClassInfoList = this.getMixedClassInfoList(taskId, teachingClassId, gradeId, courseId, isEnable);

        List<TeachingClassMergeInfo> teachingClassMergeInfoList = mixedClassInfoList.getTeachingClassMergeInfoList();


        if (CollUtil.isNotEmpty(teachingClassMergeInfoList)) {

            teachingClassMergeInfoList.forEach(o -> {
                MixClassToExcel mixClassToExcel = new MixClassToExcel();
                mixClassToExcel.setTeachingClassName(o.getTeachingClassName());
                mixClassToExcel.setCourseName(o.getCourseName());
                mixClassToExcel.setClassNum(o.getClassNum());
                mixClassToExcel.setIsEnable((o.getEnable() == 0 || o.getEnable() == 6) ? "已启用" : "未启用");

                List<WeekDto> weekDtoList = o.getWeekDtoList();

                List<MixTeachingClassExcelVo> mixTeachingClassExcelVoArrayList = new ArrayList<>();

                weekDtoList.forEach(w -> {
                    MixTeachingClassExcelVo mixTeachingClassExcelVo = new MixTeachingClassExcelVo();
                    mixClassToExcel.setAssessmentName(w.getAssessmentName());
                    if (w.getHourType().equals(1)) {
                        mixTeachingClassExcelVo.setHourType("讲授");
                    } else if (w.getHourType().equals(2)) {
                        mixTeachingClassExcelVo.setHourType("实验");
                    } else if (w.getHourType().equals(3)) {
                        mixTeachingClassExcelVo.setHourType("上机");

                    } else if (w.getHourType().equals(4)) {
                        mixTeachingClassExcelVo.setHourType("其他");
                    }

                    mixTeachingClassExcelVo.setTeacherName(w.getTeacherName());
                    mixTeachingClassExcelVo.setConnectNumber(w.getConnectNumber());
                    mixTeachingClassExcelVo.setConnectSection(w.getConnectSection());
                    mixTeachingClassExcelVo.setRoomType(ObjUtil.isNotNull(w.getRoomType()) ? GlobalConstant.roomTypeMap.get(w.getRoomType()) : "暂未设置场地类型");
                    Integer oneNumber = getOneNumber(w.getWeek());
                    mixTeachingClassExcelVo.setTotalHour(oneNumber * w.getWeekNum());
                    mixTeachingClassExcelVo.setWeekNum(w.getWeekNum());
                    mixTeachingClassExcelVo.setWeek((w.getWeek().indexOf("1") + 1) + "-" + (w.getWeek().lastIndexOf("1") + 1));
                    mixTeachingClassExcelVoArrayList.add(mixTeachingClassExcelVo);

                });

                mixClassToExcel.setSelectTeachingClassExcelVoList(mixTeachingClassExcelVoArrayList);
                mixClassToExcelList.add(mixClassToExcel);
            });
        }


        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));

        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");

        MyExcelUtils.exportExcel(mixClassToExcelList,
                format,
                "小组信息",
                MixClassToExcel.class,
                "合班教学班信息导出" + time + ".xls", response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void excelToSubClass(Integer taskId, MultipartFile file, HttpServletResponse response,Integer flag) {

        List<ExcelSubClass> excelSubClassList = this.readSubClassExcel(file);

        if (CollUtil.isNotEmpty(excelSubClassList)) {
            List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId)
                    .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

            if (CollUtil.isNotEmpty(teachingClassList)) {
                List<ErrorExcelVo> errorExcelVos2 = new ArrayList<>();
                List<String> collect = teachingClassList.stream().map(TeachingClass::getTeachingClassName).distinct().collect(Collectors.toList());
                excelSubClassList.forEach(o -> {
                    if (collect.contains(o.getTeachingClassName())) {

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(1L);
                        errorExcelVo.setErrorMsg(o.getTeachingClassName() + "教学班名称重复");
                        errorExcelVos2.add(errorExcelVo);
                    }

                    if (CollUtil.isNotEmpty(errorExcelVos2)) {
                        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
                        response.setCharacterEncoding("UTF-8");
                        response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");

                        MyExcelUtils.exportExcel(errorExcelVos2,
                                "导入拆班错误信息",
                                "错误信息",
                                ErrorExcelVo.class,
                                "导入错误信息" + time + ".xls", response);
                        return;
                    }
                });
            }
        }


        if(flag==2){
            List<SubClassDto> subClassList = this.getSubClassList(taskId, null, null, null);

            if(CollUtil.isNotEmpty(subClassList)){
                List<String> subIds = subClassList.stream().map(SubClassDto::getSubId).collect(Collectors.toList());
                this.deletedSubClass(taskId, subIds);
            }
        }

        TaskInfo taskInfo = taskInfoService.getById(taskId);

        List<ErrorExcelVo> errorExcelVos = new ArrayList<>();

        int totalWeek = TimeUtils.getTotalWeek(taskInfo.getTermBeginTime(), taskInfo.getTermEndTime()).intValue();

        //获取行政班
        Map<String, ClassDtoVo> classMap = studentMapper.getClassDto(taskId, null, null)
                .stream().collect(Collectors.toMap(ClassDtoVo::getClassName, Function.identity(), (o1, o2) -> o1));


        List<ClassInfo> classInfoList = classInfoMapper.selectList(new LambdaQueryWrapper<ClassInfo>().eq(ClassInfo::getTaskId, taskId));


        //获取课程信息
        List<String> courseNames = excelSubClassList.stream().map(ExcelSubClass::getCourseName).distinct().collect(Collectors.toList());

        Map<String, Course> courseMap = courseService.lambdaQuery().eq(Course::getTaskId, taskId).eq(Course::getDeleted, Deleted.NOT.getCode())
                .in(Course::getCourseName, courseNames).list()
                .stream().collect(Collectors.toMap(Course::getCourseName, Function.identity(), (a, b) -> a));


        List<TeachingClass> teachingClasses = teachingClassMapper.getTeacherTeachingClassList(taskId);

        List<Long> teachingClassIds = teachingClasses.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

        List<TeachingClassStudent> teachingClassStudents = teachingClassStudentMapper
                .selectList(new LambdaQueryWrapper<TeachingClassStudent>()
                        .in(TeachingClassStudent::getTeachingClassId, teachingClassIds)
                        .eq(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));


        Map<String, String> gradeMap = taskToGradeMapper.selectList(new LambdaQueryWrapper<TaskToGrade>()
                .eq(TaskToGrade::getTaskId, taskId).eq(TaskToGrade::getDeleted, Deleted.NOT.getCode())).stream()
                .collect(Collectors.toMap(TaskToGrade::getGradeName, TaskToGrade::getGradeId, (o1, o2) -> o1));

        AssessmentMethod assessmentMethod = new AssessmentMethod();
        assessmentMethod = assessmentMethodService.lambdaQuery().eq(AssessmentMethod::getTaskId, taskId).eq(AssessmentMethod::getAssessmentName, "考试").one();


        //获取学生信息
        Set<ExcelStudent> studentList = studentMapper.getStudentList(taskId);
        Map<String, List<ExcelStudent>> studentClassMap = studentList.stream().collect(Collectors.groupingBy(ExcelStudent::getClassId));
        Map<String, Long> studentIdMap = studentList.stream().collect(Collectors.toMap(ExcelStudent::getBaseStudentId, ExcelStudent::getStudentId));
        //获取教师信息
        Map<String, Teacher> teacherMap = new HashMap<>();
        List<String> teacherNos = excelSubClassList.stream().map(ExcelSubClass::getTeacherNo)
                .filter(StrUtil::isNotBlank)
                .flatMap(teacherNo -> Arrays.stream(teacherNo.split("\\|")))
                .map(String::trim)
                .distinct().collect(Collectors.toList());

        if (CollUtil.isNotEmpty(teacherNos)) {
            teacherMap = teacherService.lambdaQuery().eq(Teacher::getSchoolId, taskInfo.getSchoolId())
                    .eq(Teacher::getTerm, taskInfo.getTerm()).eq(Teacher::getDeleted, Deleted.NOT.getCode())
                    .in(Teacher::getBaseTeacherId, teacherNos).list()
                    .stream().collect(Collectors.toMap(Teacher::getBaseTeacherId, Function.identity(), (o1, o2) -> o1));
        }

        //获取教室信息
        Map<String, Room> roomMap = new HashMap<>();
        List<String> roomNames = new ArrayList<>();
        List<String> list1 = excelSubClassList.stream().filter(p -> StringUtils.isNotBlank(p.getLectureRoomName())).map(ExcelSubClass::getLectureRoomName).distinct().collect(Collectors.toList());
        List<String> list2 = excelSubClassList.stream().filter(p -> StringUtils.isNotBlank(p.getExperimentalRoomName())).map(ExcelSubClass::getExperimentalRoomName).distinct().collect(Collectors.toList());
        List<String> list3 = excelSubClassList.stream().filter(p -> StringUtils.isNotBlank(p.getComputerRoomName())).map(ExcelSubClass::getComputerRoomName).distinct().collect(Collectors.toList());
        List<String> list4 = excelSubClassList.stream().filter(p -> StringUtils.isNotBlank(p.getOtherRoomName())).map(ExcelSubClass::getOtherRoomName).distinct().collect(Collectors.toList());
        roomNames.addAll(list1);
        roomNames.addAll(list2);
        roomNames.addAll(list3);
        roomNames.addAll(list4);

        roomNames = roomNames.stream().map(String::trim).distinct().collect(Collectors.toList());

        List<Integer> roomTypeList = new ArrayList<>();
        if (CollUtil.isNotEmpty(roomNames)) {
            roomMap = roomService.lambdaQuery().eq(Room::getSchoolId, taskInfo.getSchoolId())
                    .eq(Room::getTerm, taskInfo.getTerm()).eq(Room::getDeleted, Deleted.NOT.getCode())
                    .in(Room::getRoomName, roomNames).list()
                    .stream().distinct().collect(Collectors.toMap(Room::getRoomName, Function.identity(), (o1, o2) -> o1));

            roomTypeList = roomService.lambdaQuery().eq(Room::getSchoolId, taskInfo.getSchoolId())
                    .eq(Room::getTerm, taskInfo.getTerm()).eq(Room::getDeleted, Deleted.NOT.getCode())
                    .in(Room::getRoomName, roomNames).list()
                    .stream().distinct().map(Room::getRoomType).distinct().collect(Collectors.toList());
        }

        //获取校区
        Map<String, String> campusMap = roomMapper.getCampusList(taskInfo.getSchoolId(), taskInfo.getTerm())
                .stream().collect(Collectors.toMap(Campus::getCampusName, Campus::getCampusId));


        //待插入数据 教学班 教学班对应学生 教学班对应教师 教学班参数设置
        List<TeachingClass> teachingClassList = new ArrayList<>();
        List<TeachingClassStudent> teachingClassStudentList = new ArrayList<>();
        List<TeachingClassTeacher> teachingClassTeacherList = new ArrayList<>();
        List<TeachingClassWeek> teachingClassWeekList = new ArrayList<>();

        //记录当前行数
        long index = 3;
        for (ExcelSubClass excelTeachingClass : excelSubClassList) {
            //存储当前行唯一标识
            excelTeachingClass.setTeachingClassId(index);
            index++;
        }

        //错误日志
        List<Map<String, Object>> errorMapList = new ArrayList<>(excelSubClassList.size());
        StringJoiner stringJoiner;


        List<TeachingClass> sunDeletedTeachingClass = new ArrayList<>();
        //拆班教学班分组。常规拆班根据课程分组 单双号拆班根据行政班分组
        Map<String, List<ExcelSubClass>> subClassMap = excelSubClassList.stream().collect(Collectors.groupingBy(a -> a.getType() == 1 ? a.getClassNames() : a.getCourseName()));

        for (List<ExcelSubClass> value : subClassMap.values()) {
            //subClass表所需数据
            ExcelSubClass excelSubClass = value.get(0);
            if (excelSubClass.getType().equals(1)) {
                String uuid = UUID.randomUUID().toString();
                AtomicReference<Integer> totalWeekNum = new AtomicReference<>(0);
                AtomicReference<Integer> max = new AtomicReference<>(0);

                Map<String, List<ExcelSubClass>> collect2 = value.stream().collect(Collectors.groupingBy(ExcelSubClass::getCourseName));


                if (collect2.size() > 1) {
                    collect2.forEach((k, v) -> {
                        Integer count = 0;
                        for (ExcelSubClass o : v) {
                            if (ObjUtil.isNotNull(o.getLectureWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getLectureWeekNum());
                                count += o.getLectureWeekNum();
                            }
                            if (ObjUtil.isNotNull(o.getExperimentalWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getExperimentalWeekNum());
                                count += o.getExperimentalWeekNum();
                            }
                            if (ObjUtil.isNotNull(o.getComputerWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getComputerWeekNum());
                                count += o.getComputerWeekNum();
                            }
                            if (ObjUtil.isNotNull(o.getOtherWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getOtherWeekNum());
                                count += o.getOtherWeekNum();
                            }
                        }
                        max.set(Math.max(count, max.get()));
                    });
                    Integer otherWeekNum = totalWeekNum.get() - max.get();
                    Map<String, Object> error = new HashMap<>();

                    if (max.get() > otherWeekNum) {
                        stringJoiner = new StringJoiner("\n");
                        stringJoiner.add("年级" + excelSubClass.getGradeName() + "拆班班级" + excelSubClass.getClassNames() + "不满足最大课程周学时小于等于其他课程周学时之和");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("年级" + excelSubClass.getGradeName() + "拆班班级" + excelSubClass.getClassNames() + "不满足最大课程周学时小于等于其他课程周学时之和");
                        errorExcelVos.add(errorExcelVo);

                        error.put("错误原因", stringJoiner.toString());
                        errorMapList.add(error);
                        continue;
                    }
                    checkSub(value, errorExcelVos, error, errorMapList);
                } else {

                    Map<String, Object> error = new HashMap<>();
                    collect2.forEach((k, v) -> {
                        for (ExcelSubClass o : v) {
                            if (ObjUtil.isNotNull(o.getLectureWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getLectureWeekNum());
                            }

                            if (ObjUtil.isNotNull(o.getExperimentalWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getExperimentalWeekNum());
                            }

                            if (ObjUtil.isNotNull(o.getComputerWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getComputerWeekNum());
                            }

                            if (ObjUtil.isNotNull(o.getOtherWeekNum())) {
                                totalWeekNum.set(totalWeekNum.get() + o.getOtherWeekNum());
                            }


                            if (totalWeekNum.get() % 2 != 0) {
                                StringJoiner stringJoiner1 = new StringJoiner("\n");
                                stringJoiner1.add("年级" + excelSubClass.getGradeName() + "拆班班级" + excelSubClass.getClassNames() + "不满足最大课程周学时小于等于其他课程周学时之和");

                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("年级" + excelSubClass.getGradeName() + "拆班班级" + excelSubClass.getClassNames() + "不满足最大课程周学时小于等于其他课程周学时之和");
                                errorExcelVos.add(errorExcelVo);

                                error.put("错误原因", stringJoiner1.toString());
                                errorMapList.add(error);
                                break;
                            }
                        }
                    });

                    List<ExcelSubClass> collect5 = value.stream().filter(p -> p.getIsSingle().equals(1)).collect(Collectors.toList());
                    List<String> collect7 = collect5.stream().map(ExcelSubClass::getLectureRoomName).map(String::trim).distinct().collect(Collectors.toList());
                    if (!CollUtil.isEmpty(collect7) && collect7.size() == 1) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程单班讲授教室应不相同");
                        errorExcelVos.add(errorExcelVo);
                    }


                    List<String> collect8 = collect5.stream().filter(p -> StringUtils.isNotBlank(p.getExperimentalRoomName())).map(ExcelSubClass::getExperimentalRoomName).map(String::trim).distinct().collect(Collectors.toList());
                    if (!CollUtil.isEmpty(collect8) && collect8.size() == 1) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程单班实验教室应不相同");
                        errorExcelVos.add(errorExcelVo);
                    }

                    List<String> collect9 = collect5.stream().filter(p -> StringUtils.isNotBlank(p.getComputerRoomName())).map(ExcelSubClass::getComputerRoomName).map(String::trim).distinct().collect(Collectors.toList());
                    if (!CollUtil.isEmpty(collect9) && collect9.size() == 1) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程上机教室应不相同");
                        errorExcelVos.add(errorExcelVo);
                    }

                    List<String> collect10 = collect5.stream().filter(p -> StringUtils.isNotBlank(p.getOtherRoomName())).map(ExcelSubClass::getOtherRoomName).map(String::trim).distinct().collect(Collectors.toList());
                    if (!CollUtil.isEmpty(collect10) && collect10.size() == 1) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程单班其他教室应不相同");
                        errorExcelVos.add(errorExcelVo);
                    }

                    List<String> collect = value.stream().filter(p -> StringUtils.isNotBlank(p.getLectureRoomName())).map(ExcelSubClass::getLectureRoomName).distinct().collect(Collectors.toList());
                    List<String> collect1 = value.stream().filter(p -> StringUtils.isNotBlank(p.getComputerRoomName())).map(ExcelSubClass::getComputerRoomName).distinct().collect(Collectors.toList());
                    List<String> collect4 = value.stream().filter(p -> StringUtils.isNotBlank(p.getExperimentalRoomName())).map(ExcelSubClass::getExperimentalRoomName).distinct().collect(Collectors.toList());
                    List<String> collect3 = value.stream().filter(p -> StringUtils.isNotBlank(p.getOtherRoomName())).map(ExcelSubClass::getOtherRoomName).distinct().collect(Collectors.toList());

                    if ((CollUtil.isNotEmpty(collect) && collect.size() != 2)) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程单双拆班讲授教室应相同");
                        errorExcelVos.add(errorExcelVo);
                    }

                    if ((CollUtil.isNotEmpty(collect1) && collect1.size() != 2)) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程单双拆班上机教室应相同");
                        errorExcelVos.add(errorExcelVo);
                    }


                    if ((CollUtil.isNotEmpty(collect4) && collect4.size() != 2)) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程单双拆班实验教室应相同");
                        errorExcelVos.add(errorExcelVo);
                    }

                    if ((CollUtil.isNotEmpty(collect3) && collect3.size() != 2)) {
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("同课程单双拆班其他教室应相同");
                        errorExcelVos.add(errorExcelVo);
                    }
                }


                //单班级拆班
                for (ExcelSubClass excelTeachingClass : value) {

                    stringJoiner = new StringJoiner("\n");
                    List<String> studentIds = new ArrayList<>();
                    Map<String, String> studentObjectMap = formatStudentObject(excelTeachingClass.getStudentObject());
                    if (!courseMap.containsKey(excelTeachingClass.getCourseName().trim())) {
                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行课程名称不存在:" + excelTeachingClass.getCourseName());
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行课程名称不存在:" + excelTeachingClass.getCourseName());
                        errorExcelVos.add(errorExcelVo);
                    }
                    //判断拆班班级名称是否正确
                    if (StrUtil.isBlank(excelTeachingClass.getClassNames())) {
                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行拆班班级名称不能为空");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行拆班班级名称不能为空");
                        errorExcelVos.add(errorExcelVo);
                    } else {
                        List<TeachingClass> teachingClasses1 = teachingClasses.stream()
                                .filter(p -> p.getGradeId().equals(gradeMap.get(excelTeachingClass.getGradeName())))
                                .filter(p -> p.getCourseName().equals(excelTeachingClass.getCourseName())).collect(Collectors.toList());

                        List<String> teachingClassCourseNames = teachingClasses1.stream()
                                .map(TeachingClass::getClassName)
                                .distinct().collect(Collectors.toList());

                        List<Long> classIds = teachingClasses1.stream().map(TeachingClass::getTeachingClassId)
                                .distinct().collect(Collectors.toList());

                        List<Long> subStudentIds = teachingClassStudents.stream().filter(p -> classIds.contains(p.getTeachingClassId()))
                                .distinct().map(TeachingClassStudent::getStudentId).distinct().collect(Collectors.toList());

                        List<String> baseStudentIds = studentList.stream().filter(p -> subStudentIds.contains(p.getStudentId()))
                                .map(ExcelStudent::getBaseStudentId).distinct().collect(Collectors.toList());


                        studentIds.addAll(baseStudentIds);


                        for (String className : excelTeachingClass.getClassNames().split("[,，]")) {
                            className = className.trim();
                            if (!teachingClassCourseNames.contains(className)) {
                                stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行拆班班级在该课程没有对应的教学班");
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行拆班班级在该课程没有对应的教学班");
                                errorExcelVos.add(errorExcelVo);
                            }
                        }

                        //待插入教学班对应学生 该行政班学生及改学号学生
                        String sids = studentObjectMap.get("studentIds");
                        if (StrUtil.isNotBlank(sids)) {
                            for (String studentId : sids.split("[,，]")) {
                                studentId = studentId.trim();
                                if (!studentIds.contains(studentId)) {
                                    stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行学生学号不存在:" + studentId);

                                    ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                    errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                    errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行学生学号不存在:" + studentId);
                                    errorExcelVos.add(errorExcelVo);
                                }
                            }
                        }
                        String classNames = studentObjectMap.get("classIds");
                        if (StrUtil.isNotBlank(classNames)) {
                            for (String className : classNames.split("[,，]")) {
                                className = className.trim();
                                for (ExcelStudent excelStudent : studentClassMap.get(classMap.get(className).getClassId())) {
                                    String baseStudentId = excelStudent.getBaseStudentId();
                                    if (!studentIds.contains(baseStudentId)) {
                                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行学生学号不存在:" + baseStudentId);
                                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行学生学号不存在:" + baseStudentId);
                                        errorExcelVos.add(errorExcelVo);
                                    }

                                }
                            }
                        }

                    }

                    //待插入教学班对应教师
                    if (StrUtil.isBlank(excelTeachingClass.getTeacherNo())) {
                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行教师编号不能为空");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行教师编号不能为空");
                        errorExcelVos.add(errorExcelVo);
                    } else {
                        for (String teacherNo : excelTeachingClass.getTeacherNo().split("[,，]")) {
                            teacherNo = teacherNo.trim();
                            if (!teacherMap.containsKey(teacherNo)) {
                                stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行教师编号不存在:" + teacherNo);
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行教师编号不存在:" + teacherNo);
                                errorExcelVos.add(errorExcelVo);
                            }
                        }
                    }
//                    拆班教学班roomId是否存在
                    if (StrUtil.isNotBlank(excelTeachingClass.getLectureRoomName()) && !roomMap.containsKey(excelTeachingClass.getLectureRoomName().trim())) {
                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行讲授上课场地不存在:" + excelTeachingClass.getLectureRoomName());
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行讲授上课场地不存在:" + excelTeachingClass.getLectureRoomName());
                        errorExcelVos.add(errorExcelVo);
                    }
                    if (StrUtil.isNotBlank(excelTeachingClass.getExperimentalRoomName()) && !roomMap.containsKey(excelTeachingClass.getExperimentalRoomName().trim())) {
                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行实验上课场地不存在:" + excelTeachingClass.getExperimentalRoomName());
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行实验上课场地不存在:" + excelTeachingClass.getExperimentalRoomName());
                        errorExcelVos.add(errorExcelVo);
                    }
                    if (StrUtil.isNotBlank(excelTeachingClass.getComputerRoomName()) && !roomMap.containsKey(excelTeachingClass.getComputerRoomName())) {
                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行上机上课场地不存在:" + excelTeachingClass.getComputerRoomName());
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行上机上课场地不存在:" + excelTeachingClass.getComputerRoomName());
                        errorExcelVos.add(errorExcelVo);
                    }
                    if (StrUtil.isNotBlank(excelTeachingClass.getOtherRoomName()) && !roomMap.containsKey(excelTeachingClass.getOtherRoomName())) {
                        stringJoiner.add("第" + excelTeachingClass.getTeachingClassId() + "行其他上课场地不存在:" + excelTeachingClass.getOtherRoomName());
                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                        errorExcelVo.setErrorMsg("第" + excelTeachingClass.getTeachingClassId() + "行其他上课场地不存在:" + excelTeachingClass.getOtherRoomName());
                        errorExcelVos.add(errorExcelVo);
                    }
                    if (stringJoiner.toString().length() == 0) {

                        List<String> baseStudentIds = new ArrayList<>();
                        List<String> collect3 = studentMapper.selectList(new LambdaQueryWrapper<Student>().in(Student::getBaseStudentId, studentIds))
                                .stream().filter(p -> p.getClassName().equals(excelTeachingClass.getClassNames()))
                                .map(Student::getBaseStudentId).distinct().collect(Collectors.toList());
                        for (String excelStudent : collect3) {
                            String baseStudentId = excelStudent.trim();
                            //根据学生单双号判断是否添加该学生
                            boolean isAdd = ObjUtil.isNull(excelTeachingClass.getIsSingle()) ||
                                    (int) (baseStudentId.charAt(baseStudentId.length() - 1)) % 2 == excelTeachingClass.getIsSingle() % 2;
                            if (!isAdd) {
                                baseStudentIds.add(baseStudentId);
                            }
                        }

                        List<String> collect = baseStudentIds.stream().distinct().collect(Collectors.toList());

                        ClassDtoVo classDtoVo = classMap.get(excelTeachingClass.getClassNames());


                        List<ClassInfo> classInfos = classInfoList.stream().filter(p -> p.getGradeId().equals(gradeMap.get(excelTeachingClass.getGradeName())))
                                .filter(p -> p.getClassName().equals(excelTeachingClass.getClassNames()))
                                .collect(Collectors.toList());

                        if (CollUtil.isNotEmpty(classInfos)) {
                            ClassInfo classInfo = classInfos.get(0);
                            List<TeachingClass> collect1 = teachingClasses.stream().filter(p -> p.getGradeId().equals(gradeMap.get(excelTeachingClass.getGradeName())))
                                    .filter(p -> p.getClassId().equals(String.valueOf(classInfo.getId()))).filter(p -> p.getCourseName().equals(excelTeachingClass.getCourseName())).distinct().collect(Collectors.toList());

                            sunDeletedTeachingClass.addAll(collect1);

                            //待插入教学班
                            teachingClassList.add(new TeachingClass().setTaskId(taskId)
                                    .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                    .setTeachingClassName(excelTeachingClass.getTeachingClassName())
                                    .setClassId(String.valueOf(classInfo.getId()))
                                    .setClassName(classInfo.getClassName())
                                    .setCourseId(courseMap.get(excelTeachingClass.getCourseName()).getCourseId())
                                    .setCourseName(excelTeachingClass.getCourseName())
                                    .setTeachingClassType(0)
                                    .setFlag(1)
                                    .setState(1)
                                    .setGradeId(classDtoVo.getGradeId())
                                    .setGradeName(classDtoVo.getGradeName())
                                    .setCampusId(campusMap.get(excelTeachingClass.getCampusName()))
                                    .setCampusName(excelTeachingClass.getCampusName())
                                    .setUniqueShiftSign(uuid)
                                    .setSubId(SubType.ODDEVEN.getCode())
                                    .setSubName(SubType.ODDEVEN.getMsg())
                                    .setIsSingle(excelTeachingClass.getIsSingle())
                                    .setIsEnable(IsEnable.ENABLE.getCode())
                                    .setClassNum(collect.size()));


                            if (StrUtil.isNotBlank(excelTeachingClass.getTeacherNo()) && StrUtil.isNotBlank(excelTeachingClass.getLectureWeek())) {
                                for (String teacherNo : excelTeachingClass.getTeacherNo().split("[,，]")) {
                                    teacherNo = teacherNo.trim(); // 去除空格
                                    teachingClassTeacherList.add(new TeachingClassTeacher()
                                            .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                            .setTeacherId(teacherMap.get(teacherNo).getTeacherId())
                                            .setTeacherName(teacherMap.get(teacherNo).getTeacherName())
                                            .setTeacherType(1));
                                }
                            }

                            if (StrUtil.isNotBlank(excelTeachingClass.getTeacherNo()) && StrUtil.isNotBlank(excelTeachingClass.getExperimentalWeek())) {
                                for (String teacherNo : excelTeachingClass.getTeacherNo().split("[,，]")) {
                                    teacherNo = teacherNo.trim(); // 去除空格
                                    teachingClassTeacherList.add(new TeachingClassTeacher()
                                            .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                            .setTeacherId(teacherMap.get(teacherNo).getTeacherId())
                                            .setTeacherName(teacherMap.get(teacherNo).getTeacherName())
                                            .setTeacherType(2));
                                }
                            }

                            if (StrUtil.isNotBlank(excelTeachingClass.getTeacherNo()) && StrUtil.isNotBlank(excelTeachingClass.getComputerWeek())) {
                                for (String teacherNo : excelTeachingClass.getTeacherNo().split("[,，]")) {
                                    teacherNo = teacherNo.trim(); // 去除空格
                                    teachingClassTeacherList.add(new TeachingClassTeacher()
                                            .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                            .setTeacherId(teacherMap.get(teacherNo).getTeacherId())
                                            .setTeacherName(teacherMap.get(teacherNo).getTeacherName())
                                            .setTeacherType(3));
                                }
                            }

                            if (StrUtil.isNotBlank(excelTeachingClass.getTeacherNo()) && StrUtil.isNotBlank(excelTeachingClass.getOtherWeek())) {
                                for (String teacherNo : excelTeachingClass.getTeacherNo().split("[,，]")) {
                                    teacherNo = teacherNo.trim(); // 去除空格
                                    teachingClassTeacherList.add(new TeachingClassTeacher()
                                            .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                            .setTeacherId(teacherMap.get(teacherNo).getTeacherId())
                                            .setTeacherName(teacherMap.get(teacherNo).getTeacherName())
                                            .setTeacherType(4));
                                }
                            }


                            //生成对应学生
                            if (CollUtil.isNotEmpty(collect)) {

                                //待插入教学班对应学生 该行政班学生及改学号学生
                                for (String studentId : collect) {

                                    studentId = studentId.trim();
                                    teachingClassStudentList.add(new TeachingClassStudent()
                                            .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                            .setStudentId(studentIdMap.get(studentId))
                                            .setFlag(1));
                                }
                            }


                            //待插入教学班参数设置
                            if (StrUtil.isNotBlank(excelTeachingClass.getLectureWeek())) {
                                teachingClassWeekList.add(new TeachingClassWeek()
                                        .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                        .setStartWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getLectureWeek(), totalWeek).indexOf('1') + 1)
                                        .setEndWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getLectureWeek(), totalWeek).lastIndexOf('1') + 1)
                                        .setWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getLectureWeek(), totalWeek))
                                        .setWeekNum(excelTeachingClass.getLectureWeekNum())
                                        .setAssessmentId(assessmentMethod.getAssessmentId())
                                        .setAssessmentName(assessmentMethod.getAssessmentName())
                                        .setStartWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getLectureWeek(), totalWeek).indexOf('1'))
                                        .setEndWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getLectureWeek(), totalWeek).lastIndexOf('1'))
                                        .setConnectSection(excelTeachingClass.getLectureConnectSection() == null ? 1 : excelTeachingClass.getLectureConnectSection())
                                        .setConnectNumber(excelTeachingClass.getLectureConnectNumber() == null ? 0 : excelTeachingClass.getLectureConnectNumber())
                                        .setRoomType(StrUtil.isNotBlank(excelTeachingClass.getLectureRoomName()) ? roomMap.get(excelTeachingClass.getLectureRoomName().trim()).getRoomType() : null)
                                        .setRoomId(StrUtil.isNotBlank(excelTeachingClass.getLectureRoomName()) ? roomMap.get(excelTeachingClass.getLectureRoomName().trim()).getRoomId() : null)
                                        .setHourType(1));
                            }
                            if (StrUtil.isNotBlank(excelTeachingClass.getExperimentalWeek())) {
                                teachingClassWeekList.add(new TeachingClassWeek()
                                        .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                        .setStartWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getExperimentalWeek(), totalWeek).indexOf('1') + 1)
                                        .setEndWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getExperimentalWeek(), totalWeek).lastIndexOf('1') + 1)
                                        .setWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getExperimentalWeek(), totalWeek))
                                        .setWeekNum(excelTeachingClass.getExperimentalWeekNum())
                                        .setAssessmentId(assessmentMethod.getAssessmentId())
                                        .setAssessmentName(assessmentMethod.getAssessmentName())
                                        .setConnectSection(excelTeachingClass.getExperimentalConnectSection() == null ? 1 : excelTeachingClass.getExperimentalConnectSection())
                                        .setConnectNumber(excelTeachingClass.getExperimentalConnectNumber() == null ? 0 : excelTeachingClass.getExperimentalConnectNumber())
                                        .setRoomType(StrUtil.isNotBlank(excelTeachingClass.getExperimentalRoomName()) ? roomMap.get(excelTeachingClass.getExperimentalRoomName().trim()).getRoomType() : null)
                                        .setRoomId(StrUtil.isNotBlank(excelTeachingClass.getExperimentalRoomName()) ? roomMap.get(excelTeachingClass.getExperimentalRoomName().trim()).getRoomId() : null)
                                        .setHourType(2));
                            }
                            if (StrUtil.isNotBlank(excelTeachingClass.getComputerWeek())) {
                                teachingClassWeekList.add(new TeachingClassWeek()
                                        .setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                        .setStartWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getComputerWeek(), totalWeek).indexOf('1') + 1)
                                        .setEndWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getComputerWeek(), totalWeek).lastIndexOf('1') + 1)
                                        .setWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getComputerWeek(), totalWeek))
                                        .setWeekNum(excelTeachingClass.getComputerWeekNum())
                                        .setAssessmentId(assessmentMethod.getAssessmentId())
                                        .setAssessmentName(assessmentMethod.getAssessmentName())
                                        .setConnectSection(excelTeachingClass.getComputerConnectSection() == null ? 1 : excelTeachingClass.getComputerConnectSection())
                                        .setConnectNumber(excelTeachingClass.getComputerConnectNumber() == null ? 0 : excelTeachingClass.getComputerConnectNumber())
                                        .setRoomType(StrUtil.isNotBlank(excelTeachingClass.getComputerRoomName()) ? roomMap.get(excelTeachingClass.getComputerRoomName().trim()).getRoomType() : null)
                                        .setRoomId(StrUtil.isNotBlank(excelTeachingClass.getComputerRoomName()) ? roomMap.get(excelTeachingClass.getComputerRoomName().trim()).getRoomId() : null)
                                        .setHourType(3));
                            }
                            if (StrUtil.isNotBlank(excelTeachingClass.getOtherWeek())) {
                                teachingClassWeekList.add(new TeachingClassWeek().setTeachingClassId(excelTeachingClass.getTeachingClassId())
                                        .setStartWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getOtherWeek(), totalWeek).indexOf('1') + 1)
                                        .setEndWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getOtherWeek(), totalWeek).lastIndexOf('1') + 1)
                                        .setWeek(ExcelFormatUtil.toBinary(excelTeachingClass.getOtherWeek(), totalWeek))
                                        .setWeekNum(excelTeachingClass.getOtherWeekNum())
                                        .setAssessmentId(assessmentMethod.getAssessmentId())
                                        .setAssessmentName(assessmentMethod.getAssessmentName())
                                        .setConnectSection(excelTeachingClass.getOtherConnectSection() == null ? 1 : excelTeachingClass.getOtherConnectSection())
                                        .setConnectNumber(excelTeachingClass.getOtherConnectNumber() == null ? 1 : excelTeachingClass.getOtherConnectNumber())
                                        .setRoomType(StrUtil.isNotBlank(excelTeachingClass.getOtherRoomName()) ? roomMap.get(excelTeachingClass.getOtherRoomName().trim()).getRoomType() : null)
                                        .setRoomId(StrUtil.isNotBlank(excelTeachingClass.getOtherRoomName()) ? roomMap.get(excelTeachingClass.getOtherRoomName().trim()).getRoomId() : null)
                                        .setHourType(4));
                            }
                        }
                    } else {
                        Map<String, Object> errorMap = new LinkedHashMap<>(2);
                        errorMap.put("原Excel行号", index);
                        errorMap.put("错误原因", stringJoiner.toString());
                        errorMapList.add(errorMap);
                    }
                }
            } else if (excelSubClass.getType().equals(2)) {
                //年级分组
                Map<String, List<ExcelSubClass>> subGradeMap = value.stream().collect(Collectors.groupingBy(ExcelSubClass::getGradeName));

                Map<String, Teacher> finalTeacherMap = teacherMap;
                Map<String, Room> finalRoomMap = roomMap;


                AssessmentMethod finalAssessmentMethod = assessmentMethod;
                List<Integer> finalRoomTypeList = roomTypeList;
                subGradeMap.forEach((g, m) -> {

                    //拆班班级名称分组
                    Map<String, List<ExcelSubClass>> subClassNamesMap = m.stream().collect(Collectors.groupingBy(ExcelSubClass::getClassNames));
                    subClassNamesMap.forEach((c, d) -> {
                        List<String> studentIds = new ArrayList<>();
                        String uuid = UUID.randomUUID().toString();
                        for (ExcelSubClass e : d) {
                            StringJoiner stringJoine = new StringJoiner("\n");
                            if (!courseMap.containsKey(e.getCourseName().trim())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行课程名称不存在:" + e.getCourseName());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行课程名称不存在:" + e.getCourseName());
                                errorExcelVos.add(errorExcelVo);
                            }
                            if (!campusMap.containsKey(e.getCampusName().trim())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行校区名称不存在:" + e.getCampusName());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行校区名称不存在:" + e.getCampusName());
                                errorExcelVos.add(errorExcelVo);
                            }
                            //判断拆班班级名称是否正确
                            if (StrUtil.isBlank(e.getClassNames())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行拆班班级名称不能为空");
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行拆班班级名称不能为空");
                                errorExcelVos.add(errorExcelVo);
                            } else {


                                List<TeachingClass> teachingClasses1 = teachingClasses.stream().filter(p -> p.getGradeId().equals(gradeMap.get(e.getGradeName())))
                                        .filter(p -> p.getCourseName().equals(e.getCourseName())).collect(Collectors.toList());

                                List<String> teachingClassCourseNames = teachingClasses1.stream()
                                        .map(TeachingClass::getClassName)
                                        .distinct().collect(Collectors.toList());

                                List<Long> classIds = teachingClasses1.stream().map(TeachingClass::getTeachingClassId)
                                        .distinct().collect(Collectors.toList());

                                List<Long> subStudentIds = teachingClassStudents.stream().filter(p -> classIds.contains(p.getTeachingClassId()))
                                        .distinct().map(TeachingClassStudent::getStudentId).distinct().collect(Collectors.toList());

                                List<String> baseStudentIds = studentList.stream().filter(p -> subStudentIds.contains(p.getStudentId()))
                                        .map(ExcelStudent::getBaseStudentId).distinct().collect(Collectors.toList());


                                studentIds.addAll(baseStudentIds);


                                for (String className : e.getClassNames().split("[,，]")) {
                                    className = className.trim();
                                    if (!teachingClassCourseNames.contains(className)) {
                                        stringJoine.add("第" + e.getTeachingClassId() + "行拆班班级在该课程没有对应的教学班");

                                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                        errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行拆班班级在该课程没有对应的教学班");
                                        errorExcelVos.add(errorExcelVo);
                                    }
                                }


                            }

                            //待插入教学班对应教师
                            if (StrUtil.isBlank(e.getTeacherNo())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行教师编号不能为空");
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行教师编号不能为空");
                                errorExcelVos.add(errorExcelVo);
                            } else {
                                for (String teacherNo : e.getTeacherNo().split("[,，]")) {
                                    teacherNo = teacherNo.trim();
                                    if (!finalTeacherMap.containsKey(teacherNo)) {
                                        stringJoine.add("第" + e.getTeachingClassId() + "行教师编号不存在:" + teacherNo);
                                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                        errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                        errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行教师编号不存在:" + teacherNo);
                                        errorExcelVos.add(errorExcelVo);
                                    }
                                }
                            }
                            //拆班教学班roomId是否存在
                            if (StrUtil.isNotBlank(e.getLectureRoomName()) && !finalRoomMap.containsKey(e.getLectureRoomName().trim())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行讲授上课场地不存在:" + e.getLectureRoomName());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行讲授上课场地不存在:" + e.getLectureRoomName());
                                errorExcelVos.add(errorExcelVo);
                            }
                            if (StrUtil.isNotBlank(e.getExperimentalRoomName()) && !finalRoomMap.containsKey(e.getExperimentalRoomName().trim())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行实验上课场地不存在:" + e.getExperimentalRoomName());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行实验上课场地不存在:" + e.getExperimentalRoomName());
                                errorExcelVos.add(errorExcelVo);
                            }
                            if (StrUtil.isNotBlank(e.getComputerRoomName()) && !finalRoomMap.containsKey(e.getComputerRoomName())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行上机上课场地不存在:" + e.getComputerRoomName());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行上机上课场地不存在:" + e.getComputerRoomName());
                                errorExcelVos.add(errorExcelVo);
                            }
                            if (StrUtil.isNotBlank(e.getOtherRoomName()) && !finalRoomMap.containsKey(e.getOtherRoomName())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行其他上课场地不存在:" + e.getOtherRoomName());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行其他上课场地不存在:" + e.getOtherRoomName());
                                errorExcelVos.add(errorExcelVo);
                            }


                            //检测roomType是否存在
                            if (ObjUtil.isNotNull(e.getLectureRoomType()) && !finalRoomTypeList.contains(e.getLectureRoomType())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行讲授上课场地类型不存在:" + e.getLectureRoomType());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行讲授上课场地类型不存在:" + e.getLectureRoomType());
                                errorExcelVos.add(errorExcelVo);
                            }
                            if (ObjUtil.isNotNull(e.getExperimentalRoomType()) && !finalRoomTypeList.contains(e.getExperimentalRoomType())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行实验上课场地类型不存在:" + e.getExperimentalRoomType());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行实验上课场地类型不存在:" + e.getExperimentalRoomType());
                                errorExcelVos.add(errorExcelVo);
                            }
                            if (ObjUtil.isNotNull(e.getComputerRoomType()) && !finalRoomTypeList.contains(e.getComputerRoomType())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行上机上课场地类型不存在:" + e.getComputerRoomType());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行上机上课场地类型不存在:" + e.getComputerRoomType());
                                errorExcelVos.add(errorExcelVo);
                            }
                            if (ObjUtil.isNotNull(e.getOtherRoomType()) && !finalRoomTypeList.contains(e.getOtherRoomType())) {
                                stringJoine.add("第" + e.getTeachingClassId() + "行其他上课场地类型不存在:" + e.getOtherRoomType());
                                ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                                errorExcelVo.setErrorIndex(excelSubClass.getTeachingClassId());
                                errorExcelVo.setErrorMsg("第" + e.getTeachingClassId() + "行其他上课场地类型不存在:" + e.getOtherRoomType());
                                errorExcelVos.add(errorExcelVo);
                            }

                            if (stringJoine.toString().length() == 0) {

                                List<String> strings = new ArrayList<>(Arrays.asList(e.getClassNames().split("[,，]")))
                                        .stream().map(String::trim).collect(Collectors.toList());


                                ExcelSubClass excelSubClass2 = d.get(0);

                                List<TeachingClass> collect1 = teachingClasses.stream().filter(p -> p.getGradeId().equals(gradeMap.get(e.getGradeName())))
                                        .filter(p -> strings.contains(p.getClassName()))
                                        .filter(p -> p.getCourseName().equals(excelSubClass2.getCourseName()))
                                        .distinct().collect(Collectors.toList());


                                sunDeletedTeachingClass.addAll(collect1);

                                List<ClassInfo> classInfos = classInfoList.stream().filter(p -> p.getGradeId()
                                        .equals(gradeMap.get(e.getGradeName())))
                                        .filter(p -> strings.contains(p.getClassName())).distinct().collect(Collectors.toList());


                                String classIds = classInfos.stream().map(ClassInfo::getId).distinct().map(String::valueOf)
                                        .collect(Collectors.joining(","));

                                List<Student> students = studentMapper.selectList(new LambdaQueryWrapper<Student>().in(Student::getBaseStudentId, studentIds));

                                List<String> collect = students.stream().filter(p -> strings.contains(p.getClassName())).distinct().map(Student::getBaseStudentId).distinct().collect(Collectors.toList());


                                int i = 0;
                                for (String studentId : collect) {
                                    studentId = studentId.trim();
                                    ExcelSubClass excelSubClass1 = d.get(i % d.size());
                                    TeachingClassStudent teachingClassStudent = new TeachingClassStudent();
                                    teachingClassStudent.setTeachingClassId(excelSubClass1.getTeachingClassId());
                                    teachingClassStudent.setStudentId(studentIdMap.get(studentId));
                                    teachingClassStudentList.add(teachingClassStudent);
                                    i++;
                                }


                                long classNumm = teachingClassStudentList.stream().filter(p -> p.getTeachingClassId().equals(e.getTeachingClassId())).distinct().count();

                                teachingClassList.add(new TeachingClass().setTaskId(taskId)
                                        .setTeachingClassId(e.getTeachingClassId())
                                        .setTeachingClassName(e.getTeachingClassName())
                                        .setClassId(classIds)
                                        .setClassName(e.getClassNames())
                                        .setCourseId(courseMap.get(e.getCourseName()).getCourseId())
                                        .setCourseName(e.getCourseName())
                                        .setTeachingClassType(0)
                                        .setFlag(1)
                                        .setState(1)
                                        .setSubId(SubType.GENERAL.getCode())
                                        .setSubName(SubType.GENERAL.getMsg())
                                        .setGradeId(gradeMap.get(e.getGradeName()))
                                        .setGradeName(e.getGradeName())
                                        .setCampusId(campusMap.get(e.getCampusName()))
                                        .setCampusName(e.getCampusName())
                                        .setUniqueShiftSign(uuid)
                                        .setIsEnable(IsEnable.ENABLE.getCode())
                                        .setClassNum((int) classNumm));


                                if (StrUtil.isNotBlank(e.getTeacherNo()) && StrUtil.isNotBlank(e.getLectureWeek())) {
                                    for (String teacherNo : e.getTeacherNo().split("[,，]")) {
                                        teacherNo = teacherNo.trim(); // 去除空格
                                        teachingClassTeacherList.add(new TeachingClassTeacher()
                                                .setTeachingClassId(e.getTeachingClassId())
                                                .setTeacherId(finalTeacherMap.get(teacherNo).getTeacherId())
                                                .setTeacherName(finalTeacherMap.get(teacherNo).getTeacherName())
                                                .setTeacherType(1));
                                    }
                                }

                                if (StrUtil.isNotBlank(e.getTeacherNo()) && StrUtil.isNotBlank(e.getExperimentalWeek())) {
                                    for (String teacherNo : e.getTeacherNo().split("[,，]")) {
                                        teacherNo = teacherNo.trim(); // 去除空格
                                        teachingClassTeacherList.add(new TeachingClassTeacher()
                                                .setTeachingClassId(e.getTeachingClassId())
                                                .setTeacherId(finalTeacherMap.get(teacherNo).getTeacherId())
                                                .setTeacherName(finalTeacherMap.get(teacherNo).getTeacherName())
                                                .setTeacherType(2));
                                    }
                                }

                                if (StrUtil.isNotBlank(e.getTeacherNo()) && StrUtil.isNotBlank(e.getComputerWeek())) {
                                    for (String teacherNo : e.getTeacherNo().split("[,，]")) {
                                        teacherNo = teacherNo.trim(); // 去除空格
                                        teachingClassTeacherList.add(new TeachingClassTeacher()
                                                .setTeachingClassId(e.getTeachingClassId())
                                                .setTeacherId(finalTeacherMap.get(teacherNo).getTeacherId())
                                                .setTeacherName(finalTeacherMap.get(teacherNo).getTeacherName())
                                                .setTeacherType(3));
                                    }
                                }

                                if (StrUtil.isNotBlank(e.getTeacherNo()) && StrUtil.isNotBlank(e.getOtherWeek())) {
                                    for (String teacherNo : e.getTeacherNo().split("[,，]")) {
                                        teacherNo = teacherNo.trim(); // 去除空格
                                        teachingClassTeacherList.add(new TeachingClassTeacher()
                                                .setTeachingClassId(e.getTeachingClassId())
                                                .setTeacherId(finalTeacherMap.get(teacherNo).getTeacherId())
                                                .setTeacherName(finalTeacherMap.get(teacherNo).getTeacherName())
                                                .setTeacherType(4));
                                    }
                                }


                                //待插入教学班参数设置
                                if (StrUtil.isNotBlank(e.getLectureWeek())) {
                                    teachingClassWeekList.add(new TeachingClassWeek()
                                            .setTeachingClassId(e.getTeachingClassId())
                                            .setStartWeek(ExcelFormatUtil.toBinary(e.getLectureWeek(), totalWeek).indexOf('1') + 1)
                                            .setEndWeek(ExcelFormatUtil.toBinary(e.getLectureWeek(), totalWeek).lastIndexOf('1') + 1)
                                            .setWeek(ExcelFormatUtil.toBinary(e.getLectureWeek(), totalWeek))
                                            .setWeekNum(e.getLectureWeekNum())
                                            .setAssessmentId(finalAssessmentMethod.getAssessmentId())
                                            .setAssessmentName(finalAssessmentMethod.getAssessmentName())
                                            .setConnectSection(e.getLectureConnectSection() == null ? 1 : e.getLectureConnectSection())
                                            .setConnectNumber(e.getLectureConnectNumber() == null ? 0 : e.getLectureConnectNumber())
                                            .setRoomType(StrUtil.isNotBlank(e.getLectureRoomName()) ? finalRoomMap.get(e.getLectureRoomName().trim()).getRoomType() : e.getLectureRoomType())
                                            .setRoomId(StrUtil.isNotBlank(e.getLectureRoomName()) ? finalRoomMap.get(e.getLectureRoomName().trim()).getRoomId() : null)
                                            .setHourType(1));
                                }
                                if (StrUtil.isNotBlank(e.getExperimentalWeek())) {
                                    teachingClassWeekList.add(new TeachingClassWeek()
                                            .setTeachingClassId(e.getTeachingClassId())
                                            .setStartWeek(ExcelFormatUtil.toBinary(e.getExperimentalWeek(), totalWeek).indexOf('1') + 1)
                                            .setEndWeek(ExcelFormatUtil.toBinary(e.getExperimentalWeek(), totalWeek).lastIndexOf('1') + 1)
                                            .setWeek(ExcelFormatUtil.toBinary(e.getExperimentalWeek(), totalWeek))
                                            .setWeekNum(e.getExperimentalWeekNum())
                                            .setAssessmentId(finalAssessmentMethod.getAssessmentId())
                                            .setAssessmentName(finalAssessmentMethod.getAssessmentName())
                                            .setConnectSection(e.getExperimentalConnectSection() == null ? 1 : e.getExperimentalConnectSection())
                                            .setConnectNumber(e.getExperimentalConnectNumber() == null ? 0 : e.getExperimentalConnectNumber())
                                            .setRoomType(StrUtil.isNotBlank(e.getExperimentalRoomName()) ? finalRoomMap.get(e.getExperimentalRoomName().trim()).getRoomType() : e.getExperimentalRoomType())
                                            .setRoomId(StrUtil.isNotBlank(e.getExperimentalRoomName()) ? finalRoomMap.get(e.getExperimentalRoomName().trim()).getRoomId() : null)
                                            .setHourType(2));
                                }
                                if (StrUtil.isNotBlank(e.getComputerWeek())) {
                                    teachingClassWeekList.add(new TeachingClassWeek()
                                            .setTeachingClassId(e.getTeachingClassId())
                                            .setStartWeek(ExcelFormatUtil.toBinary(e.getComputerWeek(), totalWeek).indexOf('1') + 1)
                                            .setEndWeek(ExcelFormatUtil.toBinary(e.getComputerWeek(), totalWeek).lastIndexOf('1') + 1)
                                            .setWeek(ExcelFormatUtil.toBinary(e.getComputerWeek(), totalWeek))
                                            .setWeekNum(e.getComputerWeekNum())
                                            .setAssessmentId(finalAssessmentMethod.getAssessmentId())
                                            .setAssessmentName(finalAssessmentMethod.getAssessmentName())
                                            .setConnectSection(e.getComputerConnectSection() == null ? 1 : e.getComputerConnectSection())
                                            .setConnectNumber(e.getComputerConnectNumber() == null ? 0 : e.getComputerConnectNumber())
                                            .setRoomType(StrUtil.isNotBlank(e.getComputerRoomName()) ? finalRoomMap.get(e.getComputerRoomName().trim()).getRoomType() : e.getComputerRoomType())
                                            .setRoomId(StrUtil.isNotBlank(e.getComputerRoomName()) ? finalRoomMap.get(e.getComputerRoomName().trim()).getRoomId() : null)
                                            .setHourType(3));
                                }
                                if (StrUtil.isNotBlank(e.getOtherWeek())) {
                                    teachingClassWeekList.add(new TeachingClassWeek().setTeachingClassId(e.getTeachingClassId())
                                            .setWeek(ExcelFormatUtil.toBinary(e.getOtherWeek(), totalWeek))
                                            .setWeekNum(e.getOtherWeekNum())
                                            .setStartWeek(ExcelFormatUtil.toBinary(e.getOtherWeek(), totalWeek).indexOf('1') + 1)
                                            .setEndWeek(ExcelFormatUtil.toBinary(e.getOtherWeek(), totalWeek).lastIndexOf('1') + 1)
                                            .setAssessmentId(finalAssessmentMethod.getAssessmentId())
                                            .setAssessmentName(finalAssessmentMethod.getAssessmentName())
                                            .setConnectSection(e.getOtherConnectSection() == null ? 1 : e.getOtherConnectSection())
                                            .setConnectNumber(e.getOtherConnectNumber() == null ? 0 : e.getOtherConnectNumber())
                                            .setRoomType(StrUtil.isNotBlank(e.getOtherRoomName()) ? finalRoomMap.get(e.getOtherRoomName().trim()).getRoomType() : e.getOtherRoomType())
                                            .setRoomId(StrUtil.isNotBlank(e.getOtherRoomName()) ? finalRoomMap.get(e.getOtherRoomName().trim()).getRoomId() : null)
                                            .setHourType(4));
                                }

                            } else {
                                Map<String, Object> errorMap = new LinkedHashMap<>(2);
                                errorMap.put("原Excel行号", e.getTeachingClassId());
                                errorMap.put("错误原因", stringJoine.toString());
                                errorMapList.add(errorMap);
                            }
                        }
                    });


                });
            }
        }


        Integer errorExcelVos1 = errorExcelVos.size();
        if (CollUtil.isNotEmpty(errorExcelVos)) {
            String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");

            MyExcelUtils.exportExcel(errorExcelVos,
                    "导入拆班错误信息",
                    "错误信息",
                    ErrorExcelVo.class,
                    "导入错误信息" + time + ".xls", response);

            throw new BizException("导入模板存在问题");
        }


        if (errorExcelVos1.equals(0)) {
            List<TeachingClass> list = teachingClassService.lambdaQuery().orderByDesc(TeachingClass::getTeachingClassId).list();

            Long teachingClassId = 0L;

            if (CollUtil.isNotEmpty(list)) {
                TeachingClass teachingClass = list.get(0);
                teachingClassId = teachingClass.getTeachingClassId();
            }

            List<Long> collect = sunDeletedTeachingClass.stream().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

            if (CollUtil.isNotEmpty(collect)) {

                //删除之前的设置

                teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>().eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, collect)
                        .set(TeachingClass::getDeleted, Deleted.YES.getCode()));


                teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, collect)
                        .set(TeachingClassWeek::getDeleted, Deleted.YES.getCode()));


                teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>().in(TeachingClassTeacher::getTeachingClassId, collect)
                        .set(TeachingClassTeacher::getDeleted, Deleted.YES.getCode()));

                teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>().in(TeachingClassStudent::getTeachingClassId, collect)
                        .set(TeachingClassStudent::getDeleted, Deleted.YES.getCode()));
            }


            Long finalTeachingClassId = teachingClassId;
            teachingClassList.forEach(o -> {
                o.setTeachingClassId(o.getTeachingClassId() + finalTeachingClassId);
            });

            teachingClassWeekList.forEach(o -> {
                o.setTeachingClassId(o.getTeachingClassId() + finalTeachingClassId);
            });

            teachingClassTeacherList.forEach(o -> {
                o.setTeachingClassId(o.getTeachingClassId() + finalTeachingClassId);
            });

            teachingClassStudentList.forEach(o -> {
                o.setTeachingClassId(o.getTeachingClassId() + finalTeachingClassId);
            });

            teachingClassService.saveBatch(teachingClassList);
            teachingClassWeekService.saveOrUpdateBatch(teachingClassWeekList);
            teachingClassTeacherService.saveOrUpdateBatch(teachingClassTeacherList);
            List<TeachingClassStudent> classStudents = teachingClassStudentList.stream().distinct().collect(Collectors.toList());
            teachingClassStudentService.saveOrUpdateBatch(classStudents);
        }

    }

    private void checkSub(List<ExcelSubClass> value, List<ErrorExcelVo> errorExcelVos, Map<String, Object> error, List<Map<String, Object>> errorMapList) {
        value.forEach(o -> {
            value.forEach(e -> {
                if (!o.getCourseName().equals(e.getCourseName())) {
                    if (o.getTeacherNo().equals(e.getTeacherNo())) {

                        StringJoiner stringJoiner1 = new StringJoiner("\n");

                        stringJoiner1.add("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "教师设置不可以相同");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(o.getTeachingClassId());
                        errorExcelVo.setErrorMsg("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "教师设置不可以相同");
                        errorExcelVos.add(errorExcelVo);

                        error.put("错误原因", stringJoiner1.toString());
                        errorMapList.add(error);
                    }

                    if (StringUtils.isNotBlank(o.getLectureRoomName()) && StringUtils.isNotBlank(e.getLectureRoomName()) && o.getLectureRoomName().equals(e.getLectureRoomName())) {

                        StringJoiner stringJoiner2 = new StringJoiner("\n");

                        stringJoiner2.add("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "讲授教室设置不可以相同");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(o.getTeachingClassId());
                        errorExcelVo.setErrorMsg("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "讲授教室设置不可以相同");
                        errorExcelVos.add(errorExcelVo);
                        error.put("错误原因", stringJoiner2.toString());
                        errorMapList.add(error);
                    }


                    if (StringUtils.isNotBlank(o.getComputerRoomName()) && StringUtils.isNotBlank(e.getComputerRoomName()) && o.getComputerRoomName().equals(e.getComputerRoomName())) {

                        StringJoiner stringJoiner2 = new StringJoiner("\n");

                        stringJoiner2.add("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "上机教室设置不可以相同");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(o.getTeachingClassId());
                        errorExcelVo.setErrorMsg("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "上机教室设置不可以相同");
                        errorExcelVos.add(errorExcelVo);
                        error.put("错误原因", stringJoiner2.toString());
                        errorMapList.add(error);
                    }


                    if (StringUtils.isNotBlank(o.getExperimentalRoomName()) && StringUtils.isNotBlank(e.getExperimentalRoomName()) && o.getExperimentalRoomName().equals(e.getExperimentalRoomName())) {

                        StringJoiner stringJoiner2 = new StringJoiner("\n");

                        stringJoiner2.add("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "讲授教室设置不可以相同");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(o.getTeachingClassId());
                        errorExcelVo.setErrorMsg("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "讲授教室设置不可以相同");
                        errorExcelVos.add(errorExcelVo);
                        error.put("错误原因", stringJoiner2.toString());
                        errorMapList.add(error);
                    }
                    if (StringUtils.isNotBlank(o.getOtherRoomName()) && StringUtils.isNotBlank(e.getOtherRoomName()) && o.getOtherRoomName().equals(e.getOtherRoomName())) {

                        StringJoiner stringJoiner2 = new StringJoiner("\n");

                        stringJoiner2.add("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "其他教室设置不可以相同");

                        ErrorExcelVo errorExcelVo = new ErrorExcelVo();
                        errorExcelVo.setErrorIndex(o.getTeachingClassId());
                        errorExcelVo.setErrorMsg("年级" + o.getGradeName() + o.getTeachingClassName() + "与年级" + e.getGradeName() + e.getTeachingClassName() + "其他教室设置不可以相同");
                        errorExcelVos.add(errorExcelVo);
                        error.put("错误原因", stringJoiner2.toString());
                        errorMapList.add(error);
                    }
                }

            });
        });
    }

    //设置教学班教室
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String setTeachingClassRoom(Integer taskId, Integer roomType, Long teachingClassId) {

        if (ObjUtil.isNotNull(roomType)) {
            teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                    .eq(TeachingClassWeek::getTeachingClassId, teachingClassId)
                    .set(TeachingClassWeek::getRoomType, roomType).set(TeachingClassWeek::getRoomId, null));

            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>().eq(TeachingClassWeek::getTeachingClassId, teachingClassId)
                    .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));
            List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                    .eq(TeachingClassTeacher::getTeachingClassId, teachingClassId).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

            if (CollUtil.isNotEmpty(teachingClassTeacherList)) {
                Map<Integer, List<TeachingClassTeacher>> collect = teachingClassTeacherList.stream().collect(Collectors.groupingBy(TeachingClassTeacher::getTeacherType));
                if (collect.size() == teachingClassWeekList.size()) {
                    teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                            .eq(TeachingClass::getTeachingClassId, teachingClassId)
                            .set(TeachingClass::getState, 1));
                }
            }
        }

        return "教学班教室类型已成功设置";


    }


    @Override
    public String updateBatchRoomType(Integer taskId, UpdateBatchRoomType updateBatchRoomType) {

        List<Long> teachingClassId = updateBatchRoomType.getTeachingClassId();

        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>().in(TeachingClass::getTeachingClassId, teachingClassId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> collect = teachingClassList.stream().filter(p -> p.getFlag().equals(1)).distinct().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());


        teachingClassWeekService.lambdaUpdate().in(TeachingClassWeek::getTeachingClassId, collect)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                .set(TeachingClassWeek::getRoomType, updateBatchRoomType.getRoomTypeId()).set(TeachingClassWeek::getRoomId, null)
                .update();

        List<TeachingClassWeek> teachingClassWeekList1 = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, collect)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));
        List<TeachingClassTeacher> teachingClassTeacherList1 = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>().in(TeachingClassTeacher::getTeachingClassId, collect)
                .eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

        teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                .in(TeachingClass::getTeachingClassId, collect).set(TeachingClass::getState, 0));

        List<Long> updateTeachingClassIds = new ArrayList<>();
        collect.forEach(o -> {
            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekList1.stream().filter(p -> p.getTeachingClassId().equals(o)).collect(Collectors.toList());

            List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherList1.stream().filter(p -> p.getTeachingClassId().equals(o)).collect(Collectors.toList());
            Boolean flag = false;
            if (CollUtil.isNotEmpty(teachingClassWeekList)) {
                List<TeachingClassWeek> collect1 = teachingClassWeekList.stream().filter(p -> ObjUtil.isNull(p.getRoomType())).distinct().collect(Collectors.toList());
                if (!CollUtil.isEmpty(collect1)) {
                    flag = true;
                }
            }

            if (CollUtil.isEmpty(teachingClassTeacherList)) {
                flag = true;
            }

            if (CollUtil.isNotEmpty(teachingClassTeacherList)) {
                Map<Integer, List<TeachingClassTeacher>> collect2 = teachingClassTeacherList.stream().collect(Collectors.groupingBy(TeachingClassTeacher::getTeacherType));
                List<Integer> collect1 = teachingClassWeekList.stream().map(TeachingClassWeek::getHourType).distinct().collect(Collectors.toList());

                if (collect1.size() != collect2.size()) {
                    flag = true;
                }
            }
            if (!flag) {
                updateTeachingClassIds.add(o);
            }
        });

        if (CollUtil.isNotEmpty(updateTeachingClassIds)) {
            teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                    .in(TeachingClass::getTeachingClassId, updateTeachingClassIds)
                    .eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                    .set(TeachingClass::getState, 1));
        }
        return "场地类型批量设置成功";

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String setSubTeachingClassRoom(Integer taskId, Long roomId, List<Long> teachingClassId) {
        TaskInfo taskInfo = taskInfoService.getById(taskId);
        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>().eq(Room::getSchoolId, taskInfo.getSchoolId())
                .eq(Room::getTerm, taskInfo.getTerm())
                .eq(Room::getDeleted, Deleted.NOT.getCode()));
//
        List<Room> rooms = roomList.stream().filter(p -> p.getRoomId().equals(roomId)).collect(Collectors.toList());

        if (ObjUtil.isNotNull(rooms)) {
            Room room = rooms.get(0);


//            teachingClassWeekMapper.update(null,new LambdaUpdateWrapper<TeachingClassWeek>()
//                    .eq(TeachingClassWeek::getTeachingClassId,teachingClassId)
//                    .set(TeachingClassWeek::getRoomType,roomType).set(TeachingClassWeek::getRoomId,null));
            teachingClassService.lambdaUpdate().eq(TeachingClass::getTaskId, taskId)
                    .in(TeachingClass::getTeachingClassId, teachingClassId).eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                    .set(TeachingClass::getRoomId, roomId).update();

//
            List<TeachingClassWeek> list = teachingClassWeekService.lambdaQuery().in(TeachingClassWeek::getTeachingClassId, teachingClassId).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()).list();

            if (CollUtil.isNotEmpty(list)) {

                list.forEach(o -> {
                    o.setRoomId(room.getRoomId());
                    o.setRoomType(room.getRoomType());
                });

                teachingClassWeekService.saveOrUpdateBatch(list);
            }
        }

        return "教学班教室类型已成功设置";
    }

    @Override
    public List<String> checkSubClassIsEnbale(Integer taskId) {

        List<String> teachingClassName = new ArrayList<>();
        //获取全部单双号拆班
        List<TeachingClass> teachingClassList = teachingClassService.lambdaQuery()
                .eq(TeachingClass::getTaskId,taskId)
                .eq(TeachingClass::getSubId,0)
                .eq(TeachingClass::getDeleted,Deleted.NOT.getCode()).list();

        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).collect(Collectors.toList());

        List<Long> teachingClassId = new ArrayList<>();
        if(CollUtil.isNotEmpty(teachingClassIds)){
            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, teachingClassIds)
                    .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

            List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                    .in(TeachingClassTeacher::getTeachingClassId,teachingClassIds).eq(TeachingClassTeacher::getDeleted,Deleted.NOT.getCode()));

            teachingClassIds.forEach(o->{
                List<TeachingClassWeek> teachingClassWeeks = teachingClassWeekList.stream().filter(p -> p.getTeachingClassId().equals(o) && ObjUtil.isNull(p.getRoomType())).collect(Collectors.toList());
                if(!CollUtil.isEmpty(teachingClassWeeks)){
                    teachingClassId.add(o);
                }

                List<TeachingClassTeacher> teachingClassTeachers = teachingClassTeacherList.stream().filter(p -> p.getTeachingClassId().equals(o)).collect(Collectors.toList());
                if(CollUtil.isEmpty(teachingClassTeachers)){
                    teachingClassId.add(o);
                }
            });
        }





        if(CollUtil.isNotEmpty(teachingClassId)){
            List<Long> teachingClassIds1 = teachingClassId.stream().distinct().collect(Collectors.toList());

            List<TeachingClass> teachingClasses = teachingClassList.stream().filter(p -> teachingClassIds1.contains(p.getTeachingClassId())).collect(Collectors.toList());

            teachingClasses.forEach(o->{
                teachingClassName.add(o.getTeachingClassName());
            });
        }
        return teachingClassName;



    }

    @Override
    public String updateBatchRoomId(Integer taskId, UpdateBatchRoom updateBatchRoom) {
        List<Long> teachingClassId = updateBatchRoom.getTeachingClassId();
        List<TeachingClass> teachingClassList = teachingClassMapper.
                selectList(new LambdaQueryWrapper<TeachingClass>().in(TeachingClass::getTeachingClassId, teachingClassId)
                        .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> collect = teachingClassList.stream().filter(p -> p.getFlag().equals(1))
                .distinct().map(TeachingClass::getTeachingClassId).distinct().collect(Collectors.toList());

        if (ObjUtil.isNotNull(updateBatchRoom.getRoomId())) {
            Room room = roomMapper.selectOne(new LambdaQueryWrapper<Room>().eq(Room::getRoomId, updateBatchRoom.getRoomId()));
            if (room != null) {
                teachingClassWeekService.lambdaUpdate().in(TeachingClassWeek::getTeachingClassId, collect)
                        .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                        .eq(ObjUtil.isNotNull(updateBatchRoom.getHourType()), TeachingClassWeek::getHourType, updateBatchRoom.getHourType())
                        .set(TeachingClassWeek::getRoomType, room.getRoomType())
                        .set(TeachingClassWeek::getRoomId, updateBatchRoom.getRoomId())
                        .update();
            }
        } else {
            teachingClassWeekService.lambdaUpdate().in(TeachingClassWeek::getTeachingClassId, collect)
                    .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode())
                    .eq(ObjUtil.isNotNull(updateBatchRoom.getHourType()), TeachingClassWeek::getHourType, updateBatchRoom.getHourType())
                    .set(TeachingClassWeek::getRoomType, updateBatchRoom.getRoomType())
                    .set(TeachingClassWeek::getRoomId, null)
                    .update();
        }

        List<TeachingClassWeek> teachingClassWeekList1 = teachingClassWeekMapper.selectList(new LambdaQueryWrapper<TeachingClassWeek>().in(TeachingClassWeek::getTeachingClassId, collect)
                .eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));
        List<TeachingClassTeacher> teachingClassTeacherList1 = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>().in(TeachingClassTeacher::getTeachingClassId, collect)
                .eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));

        teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                .in(TeachingClass::getTeachingClassId, collect).set(TeachingClass::getState, 0));

        List<Long> updateTeachingClassIds = new ArrayList<>();
        collect.forEach(o -> {
            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekList1.stream().filter(p -> p.getTeachingClassId().equals(o)).collect(Collectors.toList());

            List<TeachingClassTeacher> teachingClassTeacherList = teachingClassTeacherList1.stream().filter(p -> p.getTeachingClassId().equals(o)).collect(Collectors.toList());
            Boolean flag = false;
            if (CollUtil.isNotEmpty(teachingClassWeekList)) {
                List<TeachingClassWeek> collect1 = teachingClassWeekList.stream().filter(p -> ObjUtil.isNull(p.getRoomType())).distinct().collect(Collectors.toList());
                if (!CollUtil.isEmpty(collect1)) {
                    flag = true;
                }
            }

            if (CollUtil.isEmpty(teachingClassTeacherList)) {
                flag = true;
            }

            if (CollUtil.isNotEmpty(teachingClassTeacherList)) {
                Map<Integer, List<TeachingClassTeacher>> collect2 = teachingClassTeacherList.stream().collect(Collectors.groupingBy(TeachingClassTeacher::getTeacherType));
                List<Integer> collect1 = teachingClassWeekList.stream().map(TeachingClassWeek::getHourType).distinct().collect(Collectors.toList());

                if (collect1.size() != collect2.size()) {
                    flag = true;
                }
            }
            if (!flag) {
                updateTeachingClassIds.add(o);
            }
        });

        if (CollUtil.isNotEmpty(updateTeachingClassIds)) {
            teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                    .in(TeachingClass::getTeachingClassId, updateTeachingClassIds)
                    .eq(TeachingClass::getDeleted, Deleted.NOT.getCode())
                    .set(TeachingClass::getState, 1));
        }


        return "场地类型批量设置成功";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteMergeTeachingClass(Integer taskId, List<Long> mergeTeachingClass) {
        List<TeachingClass> list = teachingClassService.lambdaQuery().eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, mergeTeachingClass).list();
        List<Long> teachingClassIds = new ArrayList<>();
        list.forEach(o -> {
            ArrayList<String> strings = new ArrayList<>(Arrays.asList(o.getReserve().split(",")));
            List<Long> collect = strings.stream().map(Long::valueOf).distinct().collect(Collectors.toList());
            teachingClassIds.addAll(collect);
        });


        if (CollUtil.isNotEmpty(teachingClassIds)) {
            teachingClassMapper.update(null, new LambdaUpdateWrapper<TeachingClass>()
                    .eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, teachingClassIds)
                    .set(TeachingClass::getDeleted, Deleted.NOT.getCode()));

            teachingClassWeekMapper.update(null, new LambdaUpdateWrapper<TeachingClassWeek>()
                    .in(TeachingClassWeek::getTeachingClassId, teachingClassIds)
                    .set(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()));

            teachingClassStudentMapper.update(null, new LambdaUpdateWrapper<TeachingClassStudent>()
                    .in(TeachingClassStudent::getTeachingClassId, teachingClassIds)
                    .set(TeachingClassStudent::getDeleted, Deleted.NOT.getCode()));


            teachingClassTeacherMapper.update(null, new LambdaUpdateWrapper<TeachingClassTeacher>()
                    .in(TeachingClassTeacher::getTeachingClassId, teachingClassIds)
                    .set(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));
        }

        //删除合班信息
        teachingClassMapper.delete(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId).in(TeachingClass::getTeachingClassId, mergeTeachingClass));

        teachingClassWeekMapper.delete(new LambdaQueryWrapper<TeachingClassWeek>()
                .in(TeachingClassWeek::getTeachingClassId, mergeTeachingClass));

        teachingClassStudentMapper.delete(new LambdaQueryWrapper<TeachingClassStudent>()
                .in(TeachingClassStudent::getTeachingClassId, mergeTeachingClass));


        teachingClassTeacherMapper.delete(new LambdaQueryWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, mergeTeachingClass));


        return "合班已删除";


    }

    @Override
    public TeachingClassByClass getClassInfoListV2(Integer taskId, String gradeId, Integer typeId, Long
            courseId, Long classId, Integer isEnable, Integer pageSize, Integer currentPage) {

        /**
         * 获取不选课不拆班行政班对应的
         */
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(StringUtils.isNotBlank(gradeId), TeachingClass::getGradeId, gradeId)
                .eq(ObjUtil.isNotEmpty(typeId), TeachingClass::getFlag, typeId)
                .eq(ObjUtil.isNotEmpty(courseId), TeachingClass::getCourseId, courseId)
                .eq(ObjUtil.isNotEmpty(isEnable), TeachingClass::getIsEnable, isEnable)
                .isNotNull(TeachingClass::getClassName)
                .eq(ObjUtil.isNotEmpty(classId), TeachingClass::getClassId, String.valueOf(classId))
                .isNull(TeachingClass::getUniqueShiftSign)
                .eq(TeachingClass::getTeachingClassType, TeachingClassType.NOSELECT.getCode())
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));

        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId)
                .distinct().collect(Collectors.toList());

        if (CollUtil.isEmpty(teachingClassIds)) {

            TeachingClassByClass teachingClassByClass = new TeachingClassByClass();

            teachingClassByClass.setClassNum(0);
            teachingClassByClass.setUnable(0);
            teachingClassByClass.setEnable(0);
            teachingClassByClass.setClassInforList(new ArrayList<>());

            return teachingClassByClass;
        }


        TaskInfo taskInfo = taskInfoService.getById(taskId);


        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>().eq(Room::getSchoolId, taskInfo.getSchoolId())
                .eq(Room::getTerm, taskInfo.getTerm()));


        Page<TeachingClassWeek> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);

        Page<TeachingClassWeek> teachingClassWeekPage = teachingClassWeekMapper.getTeachingClassWeek(page, gradeId, typeId, classId == null ? null : String.valueOf(classId), isEnable, taskId, ObjUtil.isNotNull(courseId) ? courseId : null);


        List<TeachingClassWeek> records = teachingClassWeekPage.getRecords();


        List<Long> collect = records.stream().map(TeachingClassWeek::getTeachingClassId).distinct().collect(Collectors.toList());


        List<TeachingClass> collect1 = teachingClassList.stream().filter(p -> StringUtils.isNotBlank(p.getClassName())
                && collect.contains(p.getTeachingClassId())).sorted(Comparator.comparing(TeachingClass::getClassName))
                .distinct().collect(Collectors.toList());

        List<Long> collect2 = collect1.stream().map(TeachingClass::getTeachingClassId).sorted(Long::compareTo).distinct().collect(Collectors.toList());


        List<TeachingClassTeacher> teachingClassTeachers = teachingClassTeacherMapper.selectList(new LambdaQueryWrapper<TeachingClassTeacher>()
                .in(TeachingClassTeacher::getTeachingClassId, collect2).eq(TeachingClassTeacher::getDeleted, Deleted.NOT.getCode()));


        TeachingClassByClass teachingClassByClass = new TeachingClassByClass();

        List<ClassInfo> classInfoList = classInfoMapper.selectList(new LambdaQueryWrapper<ClassInfo>().eq(ClassInfo::getTaskId, taskId));

        List<ClassInfor> classInforList = new ArrayList<>();


        //合班将教学计划制空
        Map<String, List<TeachingClass>> classMap = collect1.stream().sorted(Comparator.comparing(TeachingClass::getClassName)).collect(Collectors.groupingBy(TeachingClass::getClassId));


        classMap.forEach((key, value) -> {
            ClassInfor classInfor = new ClassInfor();
            classInfor.setClassId(Long.valueOf(key));

            ClassInfo classInfo = classInfoList.stream().filter(p -> p.getId().equals(Long.valueOf(key))).findFirst().orElse(null);
            if (classInfo != null) {
                classInfor.setClassNumber(classInfo.getStudentNumber());
                classInfor.setGradeId(classInfo.getGradeId());
                classInfor.setGradeName(classInfo.getGradeName());
            }
            List<CourseInformation> courseInformationList = new ArrayList<>();
            for (TeachingClass teachingClass : value) {
                if (teachingClass.getFlag() == 1) {

                    classInfor.setClassName(teachingClass.getClassName());
                    CourseInformation courseInformation = new CourseInformation();
                    courseInformation.setTeachingClassId(teachingClass.getTeachingClassId());
                    courseInformation.setTeachingClassName(teachingClass.getTeachingClassName());
                    courseInformation.setCourseId(teachingClass.getCourseId());
                    courseInformation.setCourseName(teachingClass.getCourseName());
                    courseInformation.setClassNum(teachingClass.getClassNum());
                    courseInformation.setFlag(1);
                    List<TeachingClassWeek> classWeekList = records.stream().filter(p -> p.getTeachingClassId()
                            .equals(teachingClass.getTeachingClassId())).collect(Collectors.toList());

                    List<WeekDto> weekDtoList = new ArrayList<>();

                    for (TeachingClassWeek teachingClassWeek : classWeekList) {
                        WeekDto weekDto = new WeekDto();
                        weekDto.setAssessmentId(teachingClassWeek.getAssessmentId());
                        weekDto.setAssessmentName(teachingClassWeek.getAssessmentName());
                        weekDto.setHourType(teachingClassWeek.getHourType());
                        weekDto.setRoomId(teachingClassWeek.getRoomId());
                        if (ObjUtil.isNotNull(teachingClassWeek.getRoomId())) {

                            Room room = roomList.stream().filter(p -> p.getRoomId().equals(teachingClassWeek.getRoomId())).findFirst().orElse(null);
                            if (room != null) {
                                weekDto.setRoomName(room.getRoomName());
                            }
                        }
                        weekDto.setRoomType(teachingClassWeek.getRoomType());
                        weekDto.setRoomTypeName(GlobalConstant.roomTypeMap.get(teachingClassWeek.getRoomType()));
                        weekDto.setWeek(teachingClassWeek.getWeek());
                        weekDto.setSign(teachingClassWeek.getSign());
                        weekDto.setStartWeek(teachingClassWeek.getStartWeek());
                        weekDto.setEndWeek(teachingClassWeek.getEndWeek());
                        weekDto.setConnectSection(teachingClassWeek.getConnectSection());
                        weekDto.setConnectNumber(teachingClassWeek.getConnectNumber());

                        Integer weekNum = getOneNumber(teachingClassWeek.getWeek());

                        List<TeachingClassTeacher> teacher = teachingClassTeachers.stream().filter(p -> p.getTeachingClassId()
                                .equals(teachingClass.getTeachingClassId()) && p.getTeacherType().equals(teachingClassWeek.getHourType()))
                                .collect(Collectors.toList());

                        String collect3 = teacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).collect(Collectors.joining("、"));
                        String collect4 = teacher.stream().map(TeachingClassTeacher::getTeacherName).collect(Collectors.joining("、"));

                        weekDto.setTeacherId(collect3);
                        weekDto.setTeacherName(collect4);
                        weekDto.setTotalStudyHour(weekNum * teachingClassWeek.getWeekNum());
                        weekDto.setWeekNum(teachingClassWeek.getWeekNum());
                        weekDtoList.add(weekDto);
                    }


                    courseInformation.setIsEnable(teachingClass.getIsEnable());
                    courseInformation.setWeekDtoList(weekDtoList);

                    courseInformationList.add(courseInformation);


                } else if (teachingClass.getFlag() == 2) {

                    CourseInformation courseInformation = new CourseInformation();
                    classInfor.setClassName(teachingClass.getClassName());
                    courseInformation.setTeachingClassId(teachingClass.getTeachingClassId());
                    courseInformation.setTeachingClassName(teachingClass.getTeachingClassName());
                    courseInformation.setCourseId(teachingClass.getCourseId());
                    courseInformation.setCourseName(teachingClass.getCourseName());
                    courseInformation.setClassNum(teachingClass.getClassNum());
                    courseInformation.setFlag(2);
                    List<TeachingClassWeek> classWeekList = records.stream().filter(p -> p.getTeachingClassId()
                            .equals(teachingClass.getTeachingClassId())).collect(Collectors.toList());

                    List<WeekDto> weekDtoList = new ArrayList<>();

                    for (TeachingClassWeek teachingClassWeek : classWeekList) {
                        WeekDto weekDto = new WeekDto();
                        List<TeachingClassTeacher> teacher = teachingClassTeachers.stream().filter(p -> p.getTeachingClassId()
                                .equals(teachingClass.getTeachingClassId()) && p.getTeacherType().equals(teachingClassWeek.getHourType()))
                                .collect(Collectors.toList());

                        String collect3 = teacher.stream().map(TeachingClassTeacher::getTeacherId).map(String::valueOf).collect(Collectors.joining("、"));
                        String collect4 = teacher.stream().map(TeachingClassTeacher::getTeacherName).collect(Collectors.joining("、"));

                        weekDto.setStartWeek(teachingClassWeek.getStartWeek());
                        weekDto.setEndWeek(teachingClassWeek.getEndWeek());
                        weekDto.setTeacherId(collect3);
                        weekDto.setTeacherName(collect4);
                        weekDto.setWeek(teachingClassWeek.getWeek());
                        weekDto.setSign(teachingClassWeek.getSign());
                        weekDtoList.add(weekDto);
                    }

                    courseInformation.setIsEnable(teachingClass.getIsEnable());

                    courseInformation.setWeekDtoList(weekDtoList);
                    courseInformationList.add(courseInformation);
                }
            }

            List<CourseInformation> collect3 = courseInformationList.stream().sorted(Comparator.comparing(CourseInformation::getTeachingClassId)).collect(Collectors.toList());
            classInfor.setCourseInformationList(collect3);

            int sum = value.stream().mapToInt(TeachingClass::getState).sum();
            classInfor.setEnableNum(sum == value.size() ? 1 : 0);
            classInforList.add(classInfor);
        });

        List<String> collect4 = teachingClassList.stream().map(TeachingClass::getClassId).distinct().collect(Collectors.toList());


        Map<String, List<TeachingClass>> collect5 = teachingClassList.stream().collect(Collectors.groupingBy(TeachingClass::getClassId));


        AtomicInteger count = new AtomicInteger();
        collect5.forEach((key, value) -> {
            int sum = value.stream().mapToInt(TeachingClass::getState).sum();
            if (value.size() == sum) {
                count.getAndIncrement();
            }
        });


        List<ClassInfor> collect3 = classInforList.stream().sorted(Comparator.comparing(ClassInfor::getClassName)).collect(Collectors.toList());

        teachingClassByClass.setClassInforList(collect3);
        teachingClassByClass.setEnable(count.intValue());
        teachingClassByClass.setSize(records.size());
        teachingClassByClass.setTotalSize(teachingClassWeekPage.getTotal());
        teachingClassByClass.setUnable(collect4.size() - count.intValue());
        teachingClassByClass.setClassNum(collect4.size());

        return teachingClassByClass;
    }


    private List<ExcelTeachingDto> readTeachingClassToExcel(MultipartFile file) {
        List<ExcelTeachingDto> excelTeachingClassDtos = new ArrayList<>();

        List<String> expectedHeaders = Arrays.asList(
                "教学班名称", "课程名称", "对应行政班名称", "教学班上课对象", "年级名称", "是否需要选课", "校区名称", "课程/环节标识", "授课类型",
                "开课周", "周学时", "连上节次", "连上次数", "场地类型", "上课场地", "教师工号");
        try {
            InputStream inputStream = file.getResource().getInputStream();
            ExcelReader reader = ExcelUtil.getReader(inputStream, 0);
            //读取表头 判断是否符合预期
            List<String> actualHeaders = reader.readRow(1).stream()
                    .map(Object::toString).filter(StringUtils::isNotBlank).collect(Collectors.toList());
            if (!expectedHeaders.equals(actualHeaders)) {
                throw new BizException(5, "导入模板不匹配，请按照标准模板编辑后导入");
            }

            reader.addHeaderAlias("教学班名称", "teachingClassName");
            reader.addHeaderAlias("课程名称", "courseName");
            reader.addHeaderAlias("对应行政班名称", "classNames");
            reader.addHeaderAlias("教学班上课对象", "studentObject");
            reader.addHeaderAlias("年级名称", "gradeName");
            reader.addHeaderAlias("是否需要选课", "type");
            reader.addHeaderAlias("校区名称", "campusName");
            reader.addHeaderAlias("课程/环节标识", "flag");
            reader.addHeaderAlias("授课类型", "hourType");

            reader.addHeaderAlias("开课周", "lectureWeek");
            reader.addHeaderAlias("周学时", "lectureWeekNum");
            reader.addHeaderAlias("连上节次", "lectureConnectSection");
            reader.addHeaderAlias("连上次数", "lectureConnectNumber");
            reader.addHeaderAlias("场地类型", "lectureRoomType");
            reader.addHeaderAlias("上课场地", "lectureRoomName");
            reader.addHeaderAlias("教师工号", "lectureTeacherNo");

            excelTeachingClassDtos = reader.read(1, 2, ExcelTeachingDto.class);
            reader.close();
        } catch (IOException e) {
            log.info("excel导入教学任务失败:{}", e.getMessage());
        }
        if (excelTeachingClassDtos.size() == 0) {
            throw new BizException(5, "导入数据为空，请检查后重新导入");
        }
        return excelTeachingClassDtos;


    }

    private Map<String, String> formatStudentObject(String studentObject) {
        Map<String, String> result = new HashMap<>();
        result.put("studentIds", "");
        result.put("classIds", "");
        String[] parts = studentObject.split(";");
        for (String part : parts) {
            String[] subParts = part.split("\\|");
            if (subParts.length == 2) {
                String type = subParts[0];
                String values = subParts[1];
                if ("C".equals(type)) {
                    result.put("classIds", values);
                } else if ("S".equals(type)) {
                    result.put("studentIds", values);
                }
            }
        }
        return result;
    }

    /**
     * 读取拆班excel 返回教学班信息
     *
     * @param file
     * @return
     */
    private List<ExcelSubClass> readSubClassExcel(MultipartFile file) {
        List<ExcelSubClass> excelTeachingClasses = new ArrayList<>();
        List<String> expectedHeaders = Arrays.asList(
                "教学班名称", "课程名称", "拆班类型", "拆班班级名称", "年级名称", "上课对象", "学生单双号", "任课教师工号", "校区",
                "讲授开课周", "讲授周学时", "讲授连上节次", "讲授连上次数", "讲授场地类型", "讲授上课场地",
                "实验开课周", "实验周学时", "实验连上节次", "实验连上次数", "实验场地类型", "实验上课场地",
                "上机开课周", "上机周学时", "上机连上节次", "上机连上次数", "上机场地类型", "上机上课场地",
                "其他开课周", "其他周学时", "其他连上节次", "其他连上次数", "其他场地类型", "其他上课场地");
        try {

            InputStream inputStream = file.getResource().getInputStream();
            ExcelReader reader = ExcelUtil.getReader(inputStream, 0);
            //读取表头 判断是否符合预期
            List<String> actualHeaders = reader.readRow(1).stream()
                    .map(Object::toString).filter(StringUtils::isNotBlank)
                    .collect(Collectors.toList());
            if (!expectedHeaders.equals(actualHeaders)) {
                throw new BizException(5, "导入模板不匹配，请按照标准模板编辑后导入");
            }

            reader.addHeaderAlias("教学班名称", "teachingClassName");
            reader.addHeaderAlias("课程名称", "courseName");
            reader.addHeaderAlias("拆班类型", "type");
            reader.addHeaderAlias("拆班班级名称", "classNames");
            reader.addHeaderAlias("年级名称", "gradeName");
            reader.addHeaderAlias("上课对象", "studentObject");
            reader.addHeaderAlias("学生单双号", "isSingle");
            reader.addHeaderAlias("任课教师工号", "teacherNo");
            reader.addHeaderAlias("校区", "campusName");

            reader.addHeaderAlias("讲授开课周", "lectureWeek");
            reader.addHeaderAlias("讲授周学时", "lectureWeekNum");
            reader.addHeaderAlias("讲授连上节次", "lectureConnectSection");
            reader.addHeaderAlias("讲授连上次数", "lectureConnectNumber");
            reader.addHeaderAlias("讲授场地类型", "lectureRoomType");
            reader.addHeaderAlias("讲授上课场地", "lectureRoomName");

            reader.addHeaderAlias("实验开课周", "experimentalWeek");
            reader.addHeaderAlias("实验周学时", "experimentalWeekNum");
            reader.addHeaderAlias("实验连上节次", "experimentalConnectSection");
            reader.addHeaderAlias("实验连上次数", "experimentalConnectNumber");
            reader.addHeaderAlias("实验场地类型", "experimentalRoomType");
            reader.addHeaderAlias("实验上课场地", "experimentalRoomName");

            reader.addHeaderAlias("上机开课周", "computerWeek");
            reader.addHeaderAlias("上机周学时", "computerWeekNum");
            reader.addHeaderAlias("上机连上节次", "computerConnectSection");
            reader.addHeaderAlias("上机连上次数", "computerConnectNumber");
            reader.addHeaderAlias("上机场地类型", "computerRoomType");
            reader.addHeaderAlias("上机上课场地", "computerRoomName");

            reader.addHeaderAlias("其他开课周", "otherWeek");
            reader.addHeaderAlias("其他周学时", "otherWeekNum");
            reader.addHeaderAlias("其他连上节次", "otherConnectSection");
            reader.addHeaderAlias("其他连上次数", "otherConnectNumber");
            reader.addHeaderAlias("其他场地类型", "otherRoomType");
            reader.addHeaderAlias("其他上课场地", "otherRoomName");

            excelTeachingClasses = reader.read(1, 2, ExcelSubClass.class);
            reader.close();
        } catch (Exception ex) {
            log.info("excel导入教学任务失败:{}", ex.getMessage());
        }

        if (excelTeachingClasses.size() == 0) {
            throw new BizException(5, "导入数据为空，请检查后重新导入");
        }

        return excelTeachingClasses;
    }


    private Boolean isCourseComplete(CourseInformation courseInformation) {
        if (ObjUtil.isEmpty(courseInformation.getTeacherId())
                || StringUtils.isBlank(courseInformation.getTeacherName())) {

            return false;
        }

        return true;
    }


    private Boolean isCourseComplete(TeachingClassInfo courseInformation) {
        if (ObjUtil.isEmpty(courseInformation.getTeacherId())
                || StringUtils.isBlank(courseInformation.getTeacherName())) {

            return false;
        }

        return true;
    }


    private Boolean isCourseComplete(TeachingClassSubInfo teachingClassSubInfo) {
        if (ObjUtil.isEmpty(teachingClassSubInfo.getTeacherId())
                || StringUtils.isBlank(teachingClassSubInfo.getTeacherName())) {

            return false;
        }

        return true;
    }

    private Boolean checkWeekNum(List<SubInput> subInputs) {

        if (CollUtil.isNotEmpty(subInputs) && subInputs.size() == 1) {
            return true;
        }


        int max = 0;
        int sum = 0;

        if (CollUtil.isNotEmpty(subInputs)) {
            for (SubInput subInput : subInputs) {
                max = Math.max(max, subInput.getWeekNum());
                sum = sum + subInput.getWeekNum();
            }

            int otherNum = sum - max;

            if (max <= otherNum) {
                return true;
            }
        }
        return false;
    }

    private Integer getOneNumber(String week) {
        int count = 0;
        if (week != null && !week.isEmpty()) {
            char[] chars = week.toCharArray();
            for (char aChar : chars) {
                if (aChar == '1') {
                    count++;
                }
            }
        }
        return count;
    }


}
